Easy Q: dealing with object type

Just just at xs4all.nl
Thu Feb 3 06:50:09 EST 2005


In article <SJmdnddykYoAOpzfRVn-hA at comcast.com>,
 Steven Bethard <steven.bethard at gmail.com> wrote:

> 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

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



More information about the Python-list mailing list