Are all classes new-style classes in 2.4+?

I

Isaac Rodriguez

Hi,

This is probably a very basic question, but I've been playing with new
style classes, and I cannot see any difference in behavior when a
declare a class as:

class NewStyleClass(object):

or

class NewStyleClass:

I declare property members in both and it seems to work the exact same
way. I am using Python 2.4, and I was wondering if by default, all
classes are assumed to be derived from "object". If not, can someone
point me to some place where I can learn more about new-style classes
and their advantages? All the documentation I've found is very vague.

Thanks,

- Isaac.
 
B

Bjoern Schliessmann

Isaac said:
This is probably a very basic question, but I've been playing with
new style classes, and I cannot see any difference in behavior
when a declare a class as:

class NewStyleClass(object):

or

class NewStyleClass:

Try multiple inheritance (the order of superclass selection is
different) or try using the keyword super.

Regards,


Björn
 
R

Rene Fleschenberg

Isaac said:
I declare property members in both and it seems to work the exact same
way. I am using Python 2.4, and I was wondering if by default, all
classes are assumed to be derived from "object".

No, they are not. It's just that the "basic functionality" seems to work
the same at first glance (i.e. you don't need to learn alot of new
syntax in order to switch from old-style to new-style classes).

Play around with things like dir() and type() on old-style and new-style
classes, and you will soon see differences.
If not, can someone
point me to some place where I can learn more about new-style classes
and their advantages? All the documentation I've found is very vague.

http://www.python.org/download/releases/2.2.3/descrintro/
http://www.cafepy.com/article/python_types_and_objects/python_types_and_objects.html

--
René
OpenPGP key id: 0x63B1F5DB
JID: (e-mail address removed)


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)

iEYEAREKAAYFAkWXsrQACgkQUVK8U2Ox9duAtACgw4Lqh/hBDf2FuqOt2rUBaLW7
he8AoKO742syrIj8y40IF2/2/Ps8owy/
=Na4s
-----END PGP SIGNATURE-----
 
F

Felipe Almeida Lessa

I am using Python 2.4, and I was wondering if by default, all
classes are assumed to be derived from "object".

This won't tell you advantages or disadvantages, but will show you
that the default still is the old-style:
.... pass
....
type(old())
dir(old()) ['__doc__', '__module__']

class new(object):
.... pass
....['__class__', '__delattr__', '__dict__', '__doc__',
'__getattribute__', '__hash__', '__init__', '__module__', '__new__',
'__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__',
'__weakref__']


In general, even if you don't understand the differences, it's better
to use new-style (they're new ;-). Anyway, see
http://effbot.org/pyref/new-style-and-classic-classes.htm for a little
more information.
 
S

Steven D'Aprano

Hi,

This is probably a very basic question, but I've been playing with new
style classes, and I cannot see any difference in behavior when a
declare a class as:

class NewStyleClass(object):

or

class NewStyleClass:

I declare property members in both and it seems to work the exact same
way.

Then you aren't looking very closely. Try with a calculated property.
.... def __init__(self):
.... self._n = 1
.... def getter(self):
.... return "spam " * self._n
.... def setter(self, n):
.... self._n = n
.... spam = property(getter, setter, None, None)
....'spam spam spam spam spam spam spam '

Now try with an old-style class.
.... def __init__(self):
.... self._n = 1
.... def getter(self):
.... return "spam " * self._n
.... def setter(self, n):
.... self._n = n
.... spam = property(getter, setter, None, None)
....3

Properties should not be used with old-style classes because they just
don't work correctly.
 

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

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top