Inherting from object. Or not.

M.E.Farmer mefjr75 at hotmail.com
Wed Jan 26 16:24:41 EST 2005


Hello Frans,
What you are seeing is a step on the path to unification of types and
classes.
Now we have that clear ;)
Classes that inherit from object are 'new style classes'.
They were introduced in 2.2 and they have different internal methods.
The ones that have no declaration is an 'old style class'.
http://www.python.org/doc/newstyle.html
That link may be help.
It is not required to write a class as 'class myclass(object)' yet, so
many don't. In some cases it will matter which you use. In others cases
it won't.
Be sure to try this an an interpreter promt:
Py> class NewKlass(object):
...     pass
Py> class OldKlass:
...     pass
Py> new = NewKlass()
Py> old = OldKlass()
Py> print dir(new)
Py> print dir(old)

Also new style classes are faster.
Hth,
M.E.Farmer

Frans Englich wrote:
> (Picking up a side track of the "python without OO" thread.)
>
> On Wednesday 26 January 2005 11:01, Neil Benn wrote:
>
> > I say this because you do need to be aware of the
> > 'mythical python wand' which will turn you from a bad programmer
into a
> > good programmer simply by typing 'class Klass(object):'.
>
> What is the difference between inherting form object, and not doing
it? E.g,
> what's the difference between the two following classes?
>
> class foo:
> 	pass
>
> class bar(object):
> 	pass
>
> Sometimes people inherit from it, and sometimes not. I don't see a
pattern in 
> their choices.
> 
> 
> Cheers,
> 
> 		Frans




More information about the Python-list mailing list