Re: Struct usages in Python

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alex Gusarov

    Re: Struct usages in Python

    class Event(object):
    >
    Always subclass object, unless you have a very compelling reason not to,
    or you are subclassing something else.
    >
    I've thought that if I write

    class Event:
    pass

    , it'll be subclass of object too, I was wrong?

    --
    Best regards, Alex Gusarov
  • Diez B. Roggisch

    #2
    Re: Struct usages in Python

    Alex Gusarov schrieb:
    > class Event(object):
    >>
    >Always subclass object, unless you have a very compelling reason not to,
    >or you are subclassing something else.
    >>
    >
    I've thought that if I write
    >
    class Event:
    pass
    >
    , it'll be subclass of object too, I was wrong?

    Yes. That is the somewhat unfortunate difference between new-style and
    old-style classes. Use new-style if you can, and that means that
    "object" must be part of the inheritance graph.



    Diez

    Comment

    • Arnaud Delobelle

      #3
      Re: Struct usages in Python

      "Alex Gusarov" <alex.m.gusarov @gmail.comwrite s:
      > class Event(object):
      >>
      >Always subclass object, unless you have a very compelling reason not to,
      >or you are subclassing something else.
      >>
      >
      I've thought that if I write
      >
      class Event:
      pass
      >
      , it'll be subclass of object too, I was wrong?
      You are wrong for Python 2.X, but right for Python 3 where old-style
      classes are gone for good.

      What you define with the statement

      class Event: pass

      is an 'old-style' class. Witness:
      >>class Event: pass
      ...
      >>class NewEvent(object ): pass
      ...
      >>type(Event)
      <type 'classobj'>
      >>type(NewEvent )
      <type 'type'>
      >>type(Event( ))
      <type 'instance'>
      del>>type(NewEv ent())
      <class '__main__.NewEv ent'>

      All old-style classes are actually objects of type 'classobj' (they
      all have the same type!), all their instances are all of type 'instance'.
      >>type(FooBar ) == type(Event)
      True
      >>type(FooBar() ) == type(Event())
      True

      Whereas instances of new-style classes are of type their class:
      >>class NewFooBar(objec t): pass
      ...
      >>type(NewFooBa r) == type(NewEvent)
      True
      >>type(NewFooBa r()) == type(NewEvent() )
      False

      However, in python 2.X (X 2?), you can force all classes to of a
      certain type by setting the global variable '__metaclass__' . So:
      >>type(Event) # Event is an old-style class
      <type 'classobj'>
      >>__metaclass __ = type
      >>class Event: pass
      ...
      >>type(Event) # Now Event is new-style!
      <type 'type'>

      HTH

      --
      Arnaud

      Comment

      • Arnaud Delobelle

        #4
        Re: Struct usages in Python

        Arnaud Delobelle <arnodel@google mail.comwrites:
        "Alex Gusarov" <alex.m.gusarov @gmail.comwrite s:
        >
        >> class Event(object):
        >>>
        >>Always subclass object, unless you have a very compelling reason not to,
        >>or you are subclassing something else.
        >>>
        >>
        >I've thought that if I write
        >>
        >class Event:
        > pass
        >>
        >, it'll be subclass of object too, I was wrong?
        >
        You are wrong for Python 2.X, but right for Python 3 where old-style
        classes are gone for good.
        >
        What you define with the statement
        >
        class Event: pass
        >
        is an 'old-style' class. Witness:
        >
        >>class Event: pass
        ...
        >>class NewEvent(object ): pass
        ...
        >>type(Event)
        <type 'classobj'>
        >>type(NewEvent )
        <type 'type'>
        >>type(Event( ))
        <type 'instance'>
        del>>type(NewEv ent())
        <class '__main__.NewEv ent'>
        >
        All old-style classes are actually objects of type 'classobj' (they
        all have the same type!), all their instances are all of type 'instance'.
        Oops somthing disappeared in the copy/paste process:
        >>class FooBar: pass
        ...
        >>type(FooBar ) == type(Event)
        True
        >>type(FooBar() ) == type(Event())
        True
        >
        Whereas instances of new-style classes are of type their class:
        >
        >>class NewFooBar(objec t): pass
        ...
        >>type(NewFooBa r) == type(NewEvent)
        True
        >>type(NewFooBa r()) == type(NewEvent() )
        False
        >
        However, in python 2.X (X 2?), you can force all classes to of a
        certain type by setting the global variable '__metaclass__' . So:
        >
        >>type(Event) # Event is an old-style class
        <type 'classobj'>
        >>__metaclass __ = type
        >>class Event: pass
        ...
        >>type(Event) # Now Event is new-style!
        <type 'type'>
        >
        HTH
        >
        --
        Arnaud

        Comment

        • Alex Gusarov

          #5
          Re: Struct usages in Python

          Yes. That is the somewhat unfortunate difference between new-style and old-style classes.
          Use new-style if you can, and that means that "object" must be part of the inheritance graph.
          ....
          You are wrong for Python 2.X, but right for Python 3 where old-style
          >
          classes are gone for good.
          Thanks, I don't knew it before and it's a sensitive information for me.

          --
          Best regards, Alex Gusarov

          Comment

          Working...