why objects of old style classes are instances of 'object'

Discussion in 'Python' started by AlFire, Apr 17, 2008.

  1. AlFire

    AlFire Guest

    Hi,

    Q: from the subject, why objects of old style classes are instances of
    'object'?

    >>> class a():pass
    >>> A=a()
    >>> isinstance(A,object)


    True



    I would expect False


    Thx,
    Andy
    AlFire, Apr 17, 2008
    #1
    1. Advertising

  2. AlFire wrote:

    > Hi,
    >
    > Q: from the subject, why objects of old style classes are instances of
    > 'object'?
    >
    > >>> class a():pass
    > >>> A=a()
    > >>> isinstance(A,object)

    >
    > True


    Because everything is an object.

    But not everything is a newstyle-class:

    >>> class Foo: pass

    ....
    >>> isinstance(Foo, object)

    True
    >>> isinstance(Foo, type)

    False
    >>> class Bar(object): pass

    ....
    >>> isinstance(Bar, type)

    True
    >>>



    Diez
    Diez B. Roggisch, Apr 17, 2008
    #2
    1. Advertising

  3. AlFire

    AlFire Guest

    Diez B. Roggisch wrote:

    >
    > But not everything is a newstyle-class:
    >
    >>>> class Foo: pass

    > ...
    >>>> isinstance(Foo, object)

    > True
    >>>> isinstance(Foo, type)

    > False


    >>>> class Bar(object): pass

    > ...
    >>>> isinstance(Bar, type)

    > True
    >>>>

    >



    thx for explanation. but more I look at it less and less I like the
    notation of new-style-class definition. what is an added value of adding
    "(object)" since it is already an object. Any insight?

    Note: I am not a language purist, more a pragmatic who like a good style.

    Andy
    AlFire, Apr 22, 2008
    #3
  4. En Tue, 22 Apr 2008 00:49:27 -0300, AlFire
    <> escribió:

    > Diez B. Roggisch wrote:
    >
    >> But not everything is a newstyle-class:
    >>
    >>>>> class Foo: pass

    >> ...
    >>>>> isinstance(Foo, object)

    >> True
    >>>>> isinstance(Foo, type)

    >> False

    >
    >>>>> class Bar(object): pass

    >> ...
    >>>>> isinstance(Bar, type)

    >> True
    >>>>>

    >>

    >
    >
    > thx for explanation. but more I look at it less and less I like the
    > notation of new-style-class definition. what is an added value of adding
    > "(object)" since it is already an object. Any insight?


    Read Diez previous post again.
    An old-style class is an *instance* of object (as all other objects), but
    not a *subclass* of object. It does not *inherit* from object.
    A new-style class is both an instance of object and a subclass of object.
    It inherits from object. This is what the base (object) means.

    > Note: I am not a language purist, more a pragmatic who like a good style.


    We need *some* way to distinguish new-style classes from old ones. The
    (object) base is simple, doesn't require new syntax, and it's true (hmm,
    this may be a form of petitio principii)
    In Python 3.0, old-style classes are gone, and the (object) is not
    necesary: all classes inherit from object.

    --
    Gabriel Genellina
    Gabriel Genellina, Apr 22, 2008
    #4
    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. Devan L
    Replies:
    5
    Views:
    278
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,804
    Smokey Grindel
    Dec 2, 2006
  3. Kza
    Replies:
    4
    Views:
    417
    Andrew Koenig
    Mar 3, 2006
  4. Terry Reedy
    Replies:
    5
    Views:
    509
    Robert Kern
    Nov 30, 2010
  5. Ken Varn
    Replies:
    0
    Views:
    439
    Ken Varn
    Apr 26, 2004
Loading...

Share This Page