Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

new-style class or old-style class?

65 views
Skip to first unread message

Jayden

unread,
Sep 25, 2012, 10:44:04 AM9/25/12
to
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!!

Littlefield, Tyler

unread,
Sep 25, 2012, 11:02:00 AM9/25/12
to pytho...@python.org
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.

Chris Angelico

unread,
Sep 25, 2012, 11:05:23 AM9/25/12
to pytho...@python.org
On Wed, Sep 26, 2012 at 12:44 AM, Jayden <jayde...@gmail.com> 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

Steven D'Aprano

unread,
Sep 25, 2012, 12:20:44 PM9/25/12
to
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
Message has been deleted

Mark Lawrence

unread,
Sep 25, 2012, 2:45:37 PM9/25/12
to pytho...@python.org
Thanks for this reminder, my port of the J word code to Python has just
been simplified :)

--
Cheers.

Mark Lawrence.

Ramchandra Apte

unread,
Sep 26, 2012, 8:59:54 AM9/26/12
to
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

Roy Smith

unread,
Sep 26, 2012, 9:12:33 AM9/26/12
to
In article <2e8a9e88-9e7e-43f7...@googlegroups.com>,
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 :-)

wxjm...@gmail.com

unread,
Sep 26, 2012, 4:15:56 PM9/26/12
to
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 are widely 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

alex23

unread,
Sep 26, 2012, 8:30:31 PM9/26/12
to
On Sep 27, 6:15 am, wxjmfa...@gmail.com 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?
0 new messages