new-style class or old-style class?

Discussion in 'Python' started by Jayden, Sep 25, 2012.

  1. Jayden

    Jayden Guest

    In learning Python, I found there are two types of classes? Which one are widely used in new Python code? Is the new-style much better than old-style? Thanks!!
     
    Jayden, Sep 25, 2012
    #1
    1. Advertising

  2. On 9/25/2012 8:44 AM, Jayden wrote:
    > In learning Python, I found there are two types of classes? Which one are widely used in new Python code? Is the new-style much better than old-style? Thanks!!



    Perhaps this is useful:
    http://docs.python.org/reference/datamodel.html
    It's 3.3 I think.

    --
    Take care,
    Ty
    http://tds-solutions.net
    The aspen project: a barebones light-weight mud engine:
    http://code.google.com/p/aspenmud
    He that will not reason is a bigot; he that cannot reason is a fool; he that dares not reason is a slave.
     
    Littlefield, Tyler, Sep 25, 2012
    #2
    1. Advertising

  3. On Wed, Sep 26, 2012 at 12:44 AM, Jayden <> wrote:
    > In learning Python, I found there are two types of classes? Which one are widely used in new Python code? Is the new-style much better than old-style? Thanks!!


    Definitely go with new-style. In Python 3, old-style classes aren't
    supported, and the syntax that would create an old-style class in
    Python 2 will implicitly create a new-style class. (Explicitly
    subclassing object still works in Py3, so you can happily use that
    syntax for both.)

    ChrisA
     
    Chris Angelico, Sep 25, 2012
    #3
  4. On Tue, 25 Sep 2012 07:44:04 -0700, Jayden wrote:

    > In learning Python, I found there are two types of classes? Which one
    > are widely used in new Python code?


    New-style classes.

    > Is the new-style much better than old-style?


    Yes.

    Always use new-style classes, unless you have some specific reason for
    needing old-style ("classic") classes.

    Advantages of new-style classes:

    1) They are the future. In Python 3, all classes are "new-style" and
    classic classes are gone.

    2) Multiple inheritance works correctly. Multiple inheritance for classic
    classes is buggy.

    3) New-style classes support awesome features like super(), properties,
    descriptors, and __getattribute__. Old-style do not.

    The main disadvantage is that automatic delegation is a pain to do
    correctly in new-style classes, but trivially simple in classic classes.
    Still, all things considered, it's a good trade.



    --
    Steven
     
    Steven D'Aprano, Sep 25, 2012
    #4
  5. On Tue, 25 Sep 2012 07:44:04 -0700 (PDT), Jayden <>
    declaimed the following in gmane.comp.python.general:

    > In learning Python, I found there are two types of classes? Which one are widely used in new Python code? Is the new-style much better than old-style? Thanks!!


    In Python 3.x, you only have "new-style".

    In Python 2.x, it is preferable to use "new-style" as there are more
    capabilities with them, but there is still code inherited from prior
    versions using "old-style"
    --
    Wulfraed Dennis Lee Bieber AF6VN
    HTTP://wlfraed.home.netcom.com/
     
    Dennis Lee Bieber, Sep 25, 2012
    #5
  6. On 25/09/2012 17:20, Steven D'Aprano wrote:
    > On Tue, 25 Sep 2012 07:44:04 -0700, Jayden wrote:
    >
    >> In learning Python, I found there are two types of classes? Which one
    >> are widely used in new Python code?

    >
    > New-style classes.
    >
    >> Is the new-style much better than old-style?

    >
    > Yes.
    >
    > Always use new-style classes, unless you have some specific reason for
    > needing old-style ("classic") classes.
    >
    > Advantages of new-style classes:
    >
    > 1) They are the future. In Python 3, all classes are "new-style" and
    > classic classes are gone.
    >
    > 2) Multiple inheritance works correctly. Multiple inheritance for classic
    > classes is buggy.
    >
    > 3) New-style classes support awesome features like super(), properties,
    > descriptors, and __getattribute__. Old-style do not.
    >
    > The main disadvantage is that automatic delegation is a pain to do
    > correctly in new-style classes, but trivially simple in classic classes.
    > Still, all things considered, it's a good trade.
    >
    >
    >


    Thanks for this reminder, my port of the J word code to Python has just
    been simplified :)

    --
    Cheers.

    Mark Lawrence.
     
    Mark Lawrence, Sep 25, 2012
    #6
  7. On Tuesday, 25 September 2012 20:14:05 UTC+5:30, Jayden wrote:
    > In learning Python, I found there are two types of classes? Which one are widely used in new Python code? Is the new-style much better than old-style? Thanks!!


    Next time just Google your questions.
    :)
    Good luck with Python
     
    Ramchandra Apte, Sep 26, 2012
    #7
  8. Jayden

    Roy Smith Guest

    In article <>,
    Jayden <> wrote:

    > In learning Python, I found there are two types of classes? Which one are
    > widely used in new Python code? Is the new-style much better than old-style?
    > Thanks!!


    If you're just learning Python 2.x, you might as well use new-style
    classes, since that's what all classes are in 3.x.

    On the other hand, if you're just learning, it probably doesn't matter
    which kind you use. Until you get into some pretty sophisticated stuff,
    you won't notice any difference between the two.

    On the third hand, all it takes to create a new-style class is to have
    it inherit from object. It's no big deal to write

    >>> class Foo(object):


    instead of just

    >>> class Foo:


    so you might as well use new-style classes :)
     
    Roy Smith, Sep 26, 2012
    #8
  9. Jayden

    Guest

    Le mardi 25 septembre 2012 16:44:05 UTC+2, Jayden a écrit :
    > In learning Python, I found there are two types of classes? Which one arewidely used in new Python code? Is the new-style much better than old-style? Thanks!!


    Use Python 3 and classes.


    -------

    The interesting point or my question.

    Why a Python beginner arrives here and should ask about this?

    jmf
     
    , Sep 26, 2012
    #9
  10. Jayden

    alex23 Guest

    On Sep 27, 6:15 am, wrote:
    > The interesting point or my question.
    > Why a Python beginner arrives here and should ask about this?


    Would you prefer that they'd instead make some kind of false
    assumption and then post endless screeds condemning it?
     
    alex23, Sep 27, 2012
    #10
    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. dlo_olb
    Replies:
    2
    Views:
    2,311
    Fredrik Lundh
    Jun 25, 2003
  2. G Fernandes

    is this definition old-style or new-style

    G Fernandes, Feb 27, 2005, in forum: C Programming
    Replies:
    8
    Views:
    445
    Andrey Tarasevich
    Mar 1, 2005
  3. Kza
    Replies:
    4
    Views:
    422
    Andrew Koenig
    Mar 3, 2006
  4. John Henry
    Replies:
    24
    Views:
    1,020
    alex23
    May 30, 2008
  5. john
    Replies:
    8
    Views:
    372
    Nick Keighley
    Jun 22, 2010
Loading...

Share This Page