Inheritance issue...

Carl Banks pavlovevidence at gmail.com
Mon Mar 3 22:13:23 CET 2008


On Mar 3, 3:14 pm, MooMaster <ntv1... at gmail.com> wrote:
> On Mar 3, 11:49 am, "Diez B. Roggisch" <de... at nospam.web.de> wrote:
>
>
>
> > MooMaster schrieb:
>
> > > I'm trying to use inheritance to create a simple binary tree, but it's
> > > not going so well... here's what I pull from the documentation for
> > > super()
> > > "super( type[, object-or-type])
>
> > > Return the superclass of type. If the second argument is omitted the
> > > super object returned is unbound. If the second argument is an object,
> > > isinstance(obj, type) must be true. If the second argument is a type,
> > > issubclass(type2, type) must be true. super() only works for new-style
> > > classes.
>
> > The last sentence contains the important bit. You need to use
> > new-style-classes, which means they have to have the ancestor "object"
> > somewhere in their inheritance-graph.
>
> > Like this:
>
> > class Foo(object): pass
>
> > Certainly one of the somewhat uglier corners of Python...
>
> > Diez
>
> Thanks guys, I hadn't even heard of the distinction between "old" and
> "new" style classes...is this in the tutorial somewhere? I didn't see
> it in Classes...

Here's some reading material (it can get deep):

http://www.python.org/doc/newstyle/
http://www.python.org/download/releases/2.2.3/descrintro/


New style classes have been a part of Python since 2.2, but
unfortunately many documents intended for newbies don't even mention
them, which seems to cause more confusion than it prevents, such as
when newbies go Googling for code examples and see incompatible usage
of new-style classes.  For simple uses, new-style and old-style
classes behave similarly enough that it doesn't matter which you use,
but new-style classes support lots of things that old-style classes
don't, and usage of these features is getting more and more common.
Newbies who aren't being alerted to this are getting a raw deal.

Thankfully old-style classes are getting the axe in Python 3.0 and
hopefully this confusion will go away.

Until you switch to 3.0, inherit your base classes from object; it
will ensure that more recent additions like properties and super()
calls work.

class Node(object):
    ....


Carl Banks



More information about the Python-list mailing list