[Tutor] a class query

python at bdurham.com python at bdurham.com
Mon Jun 7 18:03:18 CEST 2010


Hi Mark,

>> I was surprised to see class Name() work (in Python 2.6.5 at least). Is this equivalent to class Name( object ) or does this create an old style class? Going forward into the 2.7/3.x world, is there a preferred style?

> RTFM? :)

I am reading TFM :)

Here's why I'm confused. The following paragraph from TFM seems to
indicate that old style classes are the default:

Quote: For compatibility reasons, classes are still old-style by
default. New-style classes are created by specifying another new-style
class (i.e. a type) as a parent class, or the “top-level type” object if
no other parent is needed. The behaviour of new-style classes differs
from that of old-style classes in a number of important details in
addition to what type() returns. Some of these changes are fundamental
to the new object model, like the way special methods are invoked.
Others are “fixes” that could not be implemented before for
compatibility concerns, like the method resolution order in case of
multiple inheritance.
http://docs.python.org/reference/datamodel.html#newstyle

Yet TFM for 2.6.5 shows all class examples without specifying a parent
class.
http://docs.python.org/tutorial/classes.html

I've been taught that the proper way to create new style classes has
been to always specify an explicit "object" parent class when creating a
new class not based on other classes. 

Somewhere along the line I seemed to have missed the fact that it is no
longer necessary to define classes with 'object' as a parent in order to
get a new style class.

In other words, it seems that the following are now equivalent:

class Name:

-AND-

class Name( object ):

My impression was the "class Name:" style created an old style class.

Thanks for your help!

Malcolm


More information about the Tutor mailing list