Inherting from object. Or not.

F

Frans Englich

(Picking up a side track of the "python without OO" thread.)

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
 
M

M.E.Farmer

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
 
L

Lee Harr

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.


I think this is the best source:
http://www.python.org/2.2.3/descrintro.html

Subclassing object gets you a "new style" class and some
additional capabilities. Maybe eventually it will not
matter.
 
P

Peter Hansen

Frans said:
What is the difference between inherting form object, and not doing it?

Although this doesn't provide a description of all the implications,
it does give you the basic answer to the question and you can easily
do further research to learn more:

http://www.python.org/doc/2.2.1/whatsnew/sect-rellinks.html#SECTION000310000000000000000

(Summary: inheriting from object gives you a "new-style" class,
not doing so gives you a classic class, and there are a
variety of differences resulting from that.)

-Peter
 
F

Frans Englich

Hello Frans,
What you are seeing is a step on the path to unification of types and
classes.

I changed all base classes in my project to inherit object. There appears to
be no reason to not do it, AFAICT.


Thanks,

Frans
 
N

Nick Coghlan

Frans said:
I changed all base classes in my project to inherit object. There appears to
be no reason to not do it, AFAICT.

Exactly. My advice is to use new-style classes unless you have a reason not to
(if you're inheriting from a builtin type, then there is no need to inherit from
object as well - the builtin types already have the correct basic type).

Cheers,
Nick.
 
N

Nick Craig-Wood

Nick Coghlan said:
Exactly. My advice is to use new-style classes unless you have a
reason not to (if you're inheriting from a builtin type, then there
is no need to inherit from object as well - the builtin types
already have the correct basic type).

Except for Exception!

Exception and anything that inherits from it is an old style class.

I discovered the other day that you can't throw a new style class as
an exception at all, eg
Traceback (most recent call last):

(not a terribly helpful message - took me a while to work it out!)

wheras old style works fine...
Traceback (most recent call last):

After that I recalled a thread on python-dev about it

http://mail.python.org/pipermail/python-dev/2004-August/046812.html
 
N

Nick Coghlan

Nick said:
Except for Exception!

Exception and anything that inherits from it is an old style class.

And 'classobj' is the correct basic type for an Exception, since, as you
mentioned, new-style classes are currently unraisable :)

Cheers,
Nick.
I think there *is* work in progress to change that. . .
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top