new-style class or old-style class?

J

Jayden

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!!
 
L

Littlefield, Tyler

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

Chris Angelico

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
 
S

Steven D'Aprano

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

Dennis Lee Bieber

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"
 
M

Mark Lawrence

New-style classes.


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 :)
 
R

Ramchandra Apte

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
 
R

Roy Smith

Jayden said:
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

instead of just

so you might as well use new-style classes :)
 
W

wxjmfauth

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
 
A

alex23

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?
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top