__bases__ and type()

Martin v. Löwis loewis at informatik.hu-berlin.de
Thu Nov 21 07:35:29 EST 2002


"Daniel Silva" <dansilva at lynx.dac.neu.edu> writes:

> User-built classes would have class as its only parent if they do not
> have inheritance, and a list of super-classes in case they do.
> 
> However, given a class C, class D, and a class E that inherits from both
> C and D, type(e) evaluates to ClassType, and e.__bases__ evaluates to
> (c,d).
> 
> Did I get the organization of things incorrectly?  How else should I
> organize it for type(), __bases__, and isinstance() to work as expected?

You should understand the difference between classes and types; since
Python 2.2, these are called old-style classes and new-style classes.

A class is an instance of the ClassType, a type is an instance of the
TypeType. Since Python 2.2, both have __bases__, but they have
slightly different semantics. One difference is that, for an instance
I of a type, "type(I) is I.__class__". For an instance of a class,
"type(I) is InstanceType", "type(I.__class__) is ClassType".  There
are more differences, like under what circumstances you can assign to
__class__.

If you want to implement the semantics of Python 2.2 correctly, you
could make your concept of "node" equivalent to that of "type" in
Python. Then, executing a class statement only creates a "node" if the
first base class also is a "node". Otherwise, you need to have a
"node" ClassType, and executing a class statement creates a an
instance thereof.

Or you could deviate from the Python semantics, and treat old-style
classes and new-style classes the same.

Regards,
Martin



More information about the Python-list mailing list