Easy Q: dealing with object type

Steve Holden steve at holdenweb.com
Thu Feb 3 13:24:23 EST 2005


Erik Johnson wrote:
>> Steven Bethard <steven.bethard at gmail.com> wrote:
>>
>>>py> class A:
>>>...     pass
>>>...
>>>py> class B:
>>>...     pass
>>>...
>>>py> a = A()
>>>py> a.__class__ == A
>>>True
>>>py> a.__class__ == B
>>>False
> 
> 
>  "Just" <just at xs4all.nl> wrote
> 
>>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. :(
> 
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
-- 
Meet the Python developers and your c.l.py favorites March 23-25
Come to PyCon DC 2005                      http://www.pycon.org/
Steve Holden                           http://www.holdenweb.com/



More information about the Python-list mailing list