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.)

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

  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

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

  3. Frans Englich

    Lee Harr Guest

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

    Peter Hansen Guest

    Frans Englich wrote:
    > 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
     
    Peter Hansen, Jan 26, 2005
    #4
  5. On Wednesday 26 January 2005 21:24, M.E.Farmer wrote:
    > 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
     
    Frans Englich, Jan 26, 2005
    #5
  6. Frans Englich

    Nick Coghlan Guest

    Frans Englich wrote:
    > On Wednesday 26 January 2005 21:24, M.E.Farmer wrote:
    >
    >>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.


    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 | | Brisbane, Australia
    ---------------------------------------------------------------
    http://boredomandlaziness.skystorm.net
     
    Nick Coghlan, Jan 27, 2005
    #6
  7. Nick Coghlan <> wrote:
    > 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

    >>> class MyException(object): pass

    ....
    >>> raise MyException

    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    TypeError: exceptions must be classes, instances, or strings (deprecated), not type
    >>>


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

    wheras old style works fine...

    >>> class MyOldException: pass

    ....
    >>> raise MyOldException

    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    __main__.MyOldException: <__main__.MyOldException instance at 0xb7df4cac>
    >>>


    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 <> -- http://www.craig-wood.com/nick
     
    Nick Craig-Wood, Jan 27, 2005
    #7
  8. Frans Englich

    Nick Coghlan Guest

    Nick Craig-Wood wrote:
    > Nick Coghlan <> wrote:
    >
    >> 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.


    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 | | Brisbane, Australia
    ---------------------------------------------------------------
    http://boredomandlaziness.skystorm.net
     
    Nick Coghlan, Jan 28, 2005
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Parthiv Joshi
    Replies:
    2
    Views:
    716
    Kalyan
    Jul 2, 2004
  2. =?Utf-8?B?QXJ0?=

    Page inherting from .master - unexpected results

    =?Utf-8?B?QXJ0?=, May 26, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    372
    =?Utf-8?B?QXJ0?=
    May 26, 2006
  3. jon wayne
    Replies:
    9
    Views:
    735
    Jim Langston
    Sep 22, 2005
  4. ThatsIT.net.au

    Inherting classes

    ThatsIT.net.au, Sep 7, 2009, in forum: ASP .Net
    Replies:
    4
    Views:
    375
    ThatsIT.net.au
    Sep 9, 2009
  5. Chuck P

    GridView Inherting

    Chuck P, Jun 25, 2007, in forum: ASP .Net Building Controls
    Replies:
    6
    Views:
    798
    Walter Wang [MSFT]
    Jul 12, 2007
Loading...

Share This Page