Inherting from object. Or not.

Discussion in 'Python' started by Frans Englich, Jan 26, 2005.

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

    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
     
    Frans Englich, Jan 26, 2005
    #1
    1. Advertisements

  2. Frans Englich

    M.E.Farmer Guest

    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
     
    M.E.Farmer, Jan 26, 2005
    #2
    1. Advertisements

  3. Frans Englich

    Lee Harr Guest

    What is the difference between inherting form object, and not doing it? E.g,

    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.
     
    Lee Harr, Jan 26, 2005
    #3
  4. Frans Englich

    Peter Hansen Guest

    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
     
    Peter Hansen, Jan 26, 2005
    #4
  5. I changed all base classes in my project to inherit object. There appears to
    be no reason to not do it, AFAICT.


    Thanks,

    Frans
     
    Frans Englich, Jan 26, 2005
    #5
  6. Frans Englich

    Nick Coghlan Guest

    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.
     
    Nick Coghlan, Jan 27, 2005
    #6
  7. 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
     
    Nick Craig-Wood, Jan 27, 2005
    #7
  8. Frans Englich

    Nick Coghlan Guest

    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. . .
     
    Nick Coghlan, Jan 28, 2005
    #8
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.