Easy Q: dealing with object type

Steven Bethard steven.bethard at gmail.com
Wed Feb 2 23:25:02 EST 2005


Erik Johnson wrote:
> "Erick" <idadesub at gmail.com> wrote in message
> news:1107396669.028096.301290 at g14g2000cwa.googlegroups.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



More information about the Python-list mailing list