Easy Q: dealing with object type

E

Erik Johnson

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..... pass
....<__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...
type(3) == int 1
ls = range(3)
ls [0, 1, 2]
type(ls)
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(obj) == A 0
type(obj) is A
0

# The following "works", but I don't want to keep a set of instances to
compare against1
 
D

Daniel Bickett

# The following "works", but I don't want to keep a set of instances to
compare against
1

How about:
True

then again....
True

they're both of type 'instance'. So how about this:
True

I believe that achieves what you were aiming for.
 
S

Steven Bethard

Erik said:
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
 
J

Just

Steven Bethard said:
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

Uh, isinstance(a, A) works for both new-style and old-style classes.
Heck, isinstance() even works in Python 1.5.2...
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...

Just
 
S

Steven Bethard

Just said:
Uh, isinstance(a, A) works for both new-style and old-style classes.
Heck, isinstance() even works in Python 1.5.2...

The OP asked "Given some object ... how do you test its type?". I
interpreted this as a strict check, not a transitive check:

py> class A:
.... pass
....
py> class B(A):
.... pass
....
py> b = B()
py> isinstance(b, B)
True
py> isinstance(b, A)
True
py> b.__class__ == B
True
py> b.__class__ == A
False

Of course, if the OP doesn't need the strict check here, isinstance is,
of course, the right answer.

STeve
 
S

Steve Holden

Erik said:
Oh, there! Not that there is anything wrong with new classes, but that
is just the sort of thing that I expected to find. No, neither of these is
bad at all. I was looking for something like the obj.__class__ attribute,
but I couldn't see it under dir(obj). So, why is _class__ magically tucked
away where you can't see it? That doesn't seem very "Pythonic".

I also looked in my two python books for instance(), or instanceof()
functions - wasn't seeing anything. Actually, now that I check the indices
of "Learning Python" 1E & "Programming Python" 2E, I don't see isinstance()
either. How unfortunate. :(
Unfortunate but, given that this "introspection" is normally considered
to be a fairly advanced language feature, hardly surprising.
As an aside, I notice a lot of other people's interpreters actually
print 'True' or 'False' where my system prints 0 or 1. Is that a
configuration that can easily set somewhere?
Nope, it's a version thing. I believe Booleans were introduced at some
odd point like 2.2.1. Until then "True" and "False" were just names like
any other.

regards
Steve
 
F

Fredrik Lundh

Erik said:
As an aside, I notice a lot of other people's interpreters actually
print 'True' or 'False' where my system prints 0 or 1. Is that a
configuration that can easily set somewhere?

$ python2.1 -c "print 1 == 1"
1

$ python2.2 -c "print 1 == 1"
1

$ python2.3 -c "print 1 == 1"
True

$ python2.4 -c "print 1 == 1"
True

</F>
 
E

Erik Johnson

"Just said:
Uh, isinstance(a, A) works for both new-style and old-style classes.
Heck, isinstance() even works in Python 1.5.2...

Oh, there! Not that there is anything wrong with new classes, but that
is just the sort of thing that I expected to find. No, neither of these is
bad at all. I was looking for something like the obj.__class__ attribute,
but I couldn't see it under dir(obj). So, why is _class__ magically tucked
away where you can't see it? That doesn't seem very "Pythonic".

I also looked in my two python books for instance(), or instanceof()
functions - wasn't seeing anything. Actually, now that I check the indices
of "Learning Python" 1E & "Programming Python" 2E, I don't see isinstance()
either. How unfortunate. :(

As an aside, I notice a lot of other people's interpreters actually
print 'True' or 'False' where my system prints 0 or 1. Is that a
configuration that can easily set somewhere?

Thanks for your help!

-ej
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,201
Latest member
KourtneyBe

Latest Threads

Top