class C: vs class C(object):

Steven D'Aprano steve at REMOVE.THIS.cybersource.com.au
Thu Jul 19 04:09:59 EDT 2007


On Thu, 19 Jul 2007 07:31:06 +0000, nvictor wrote:

> Hi,
> 
> I'm not an experienced developer, and I came across this statement by
> reading a code. I search for explanation, but can't find anything
> meaningful. I read the entire document written by python's creator
> about the features of version 2.2 The one named unifying types and
> classes. But This document only blew my head away.
> 
> I ended here and think somebody can explain me more about this. The
> only thing I have noticed is that when you do dir(C) on a classic
> class you get a bunch of attributes; and when you do the same thing on
> a class defined using class C(object), you get less attributes.
> 
> Thanks for all replies.


The short answer is, unless you are writing code that has to be backwards
compatible with very old versions of Python, always use new style classes
by inheriting from object instead of old-style classes.

It isn't wrong to use the old style, but it is deprecated, and they will
eventually go away. Old style classes are the way classes used to be, back
in Ancient Days. You couldn't inherit from built-in types like str or int,
which used a different mechanism under the hood.

Certain very useful features simply will not work correctly with old-style
classes, like properties and slots. Also, multiple inheritance works
better with new-style classes: there is a subtle design flaw in the way it
works for old-style classes, which can lead to bugs.

For the technical answer about the difference, go back and read the
document about unifying classes and types.


-- 
Steven.




More information about the Python-list mailing list