473,406 Members | 2,620 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Easy Q: dealing with object type


I quickly browsed through section 9 of the Tutorial, tried some simple
Google searches: I'm not readily seeing how to test class type. Given some
object (might be an instance of a user-created class, might be None, might
be list, might be some other "standard" type object instance), how do you
test its type?
Python 2.2.2 (#1, Mar 17 2003, 15:17:58)
[GCC 3.3 20030226 (prerelease) (SuSE Linux)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
class A: .... pass
.... obj = A()
obj <__main__.A instance at 0x81778d4>

# Here, it's fairly obvious its an A-type object. A as defined in module
'__main__'.

# Some of the comparisons against "Python primitives", work as you might
expect... int <type 'int'> type(3) == int 1 ls = range(3)
ls [0, 1, 2] type(ls) <type 'list'> type(ls) == list 1 type({}) == dict 1 type(3.14) == float 1
# but this doesn't seem to extend to user-defined classes. dir(obj) ['__doc__', '__module__'] obj.__module__ '__main__' type(obj) <type 'instance'> type(obj) == A 0 type(obj) is A 0

# The following "works", but I don't want to keep a set of instances to
compare against obj2 = A()
type(obj) == type(obj2)

1

Jul 18 '05 #1
5 1145
On Erik Johnson wrote:
# The following "works", but I don't want to keep a set of instances to
compare against
obj2 = A()
type(obj) == type(obj2) 1


How about:
class A: pass class B: pass objA = A()
type( objA ) == type( A() ) True

then again....
objB = B()
type( objA ) == type( B() ) True

they're both of type 'instance'. So how about this:
class A: pass class B( object ): pass objA = A()
objB = B()
type( objA ) <type 'instance'> type( objB ) <class '__main__.B'> type( objB ) == B

True

I believe that achieves what you were aiming for.

--
Daniel Bickett
dbickett at gmail.com
http://heureusement.org/
Jul 18 '05 #2
Ah, you're running into the "old-style classes vs. new style classes".
Try subclassing from "object".

For example:
class A(object): .... pass
.... a=A()
type(a) <class '__main__.A'> type(a) == A True type(a) is A True b=A()
type(a) == type(b) True type(a) is type(b)

True
Check out the following article, it should answer your questions:
http://www.python.org/doc/2.2.3/what...00000000000000
-e

Jul 18 '05 #3
Ah, you're running into the "old-style classes vs. new style classes".
Try subclassing from "object".

For example:
class A(object): .... pass
.... a=A()
type(a) <class '__main__.A'> type(a) == A True type(a) is A True b=A()
type(a) == type(b) True type(a) is type(b)

True
Check out the following article, it should answer your questions:
http://www.python.org/doc/2.2.3/what...00000000000000
-e

Jul 18 '05 #4

"Erick" <id******@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
Ah, you're running into the "old-style classes vs. new style classes".
Try subclassing from "object".

For example:
class A(object):

That works! :) I guess I am fortunate to be running 2.2 - looks kinda ugly
prior to that.
Check out the following article, it should answer your questions:

http://www.python.org/doc/2.2.3/what...00000000000000
Thank you both for your replies! :)
-ej
Jul 18 '05 #5
Erik Johnson wrote:
"Erick" <id******@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
Ah, you're running into the "old-style classes vs. new style classes".
Try subclassing from "object".

For example:
>class A(object):


That works! :) I guess I am fortunate to be running 2.2 - looks kinda ugly
prior to that.


It's not horrible:

py> class A:
.... pass
....
py> class B:
.... pass
....
py> a = A()
py> a.__class__ == A
True
py> a.__class__ == B
False

Still, if you can use new-style classes, you should.

Also, you should probably Google for "duck typing". Generally, in
Python it is frowned upon to check the type of an object. There are
times when it's necessary, but if you're just starting off, my guess is
that you haven't discovered one of these times yet...

Steve
Jul 18 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Oliver Bryant | last post by:
I just finished developing a javascipt component allowing floating captions to appear over HTML elements. If anyone wants to check it out you can see specs and download it from...
18
by: Mike Bartels | last post by:
Hi Everyone! I have two Arrays A and B. Both arrays are byte arrays with 7 bytes each. The contents of array A and B are the same A = {1, 2, 3, 4, 5, 6, 7}; B = {1, 2, 3, 4, 5, 6, 7}; When...
1
by: Patrick | last post by:
Hi, This post is the 'sequel' ;) of the "Data Oriented vs Object Oriented Design" post, but it can be read and treated apart from that one. I will just quote the beginning of my previous message...
1
by: Mad Scientist Jr | last post by:
can someone explain how to simply populate a grid in .net ? the way i understand it, there is no more msflexgrid, and instead is this new control that has to be tied to a dataset, and it is a real...
3
by: RC | last post by:
Let's say: if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200) { // Now I got an XML object here var xmlDocument = XMLHttpRequestObject.responseXML; // next I have...
0
markrawlingson
by: markrawlingson | last post by:
It seems that a lot of people run into this issue in regards to dealing with the result that checkboxes return to the Request.Form object. So this article is intended to fully explain how checkboxes...
1
by: =?Utf-8?B?Qi4gQ2hlcm5pY2s=?= | last post by:
(If I'm overlooking anything, please let me know.) First, my only concern is updating single records in a Detailsview using an ObjectDataSource. The target table has a timestamp field. Assume ...
0
by: raylopez99 | last post by:
I ran afoul of this Compiler error CS1612 recently, when trying to modify a Point, which I had made have a property. It's pointless to do this (initially it will compile, but you'll run into...
14
by: mojumbo | last post by:
Problem: I have a structure which needs to store its data in contiguous memory by there is a dynamic element which can't be defined at compile time. It needs to be aligned along a 4 byte...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.