By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,222 Members | 2,416 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,222 IT Pros & Developers. It's quick & easy.

Are all classes new-style classes in 2.4+?

P: n/a
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.

Dec 31 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Isaac Rodriguez wrote:
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

--
BOFH excuse #113:

Root nameservers are out of sync

Dec 31 '06 #2

P: n/a
Isaac Rodriguez wrote:
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/relea....3/descrintro/
http://www.cafepy.com/article/python...d_objects.html

--
René
OpenPGP key id: 0x63B1F5DB
JID: re***************@jabber.ccc.de
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)

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

Dec 31 '06 #3

P: n/a
On 31 Dec 2006 03:57:04 -0800, Isaac Rodriguez
<is*************@comcast.netwrote:
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:
>>class old:
.... pass
....
>>type(old())
<type 'instance'>
>>dir(old())
['__doc__', '__module__']
>>>
class new(object):
.... pass
....
>>type(new())
<class '__main__.new'>
>>dir(new())
['__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-an...ic-classes.htm for a little
more information.

--
Felipe.
Dec 31 '06 #4

P: n/a
On Sun, 31 Dec 2006 03:57:04 -0800, Isaac Rodriguez wrote:
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.
>>class New(object):
.... 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)
....
>>obj = New()
obj.spam
'spam '
>>obj.spam = 3
obj.spam
'spam spam spam '
>>obj.spam = 7
obj.spam
'spam spam spam spam spam spam spam '

Now try with an old-style class.
>>class Old:
.... 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)
....
>>obj = Old()
obj.spam
'spam '
>>obj.spam = 3
obj.spam
3

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

--
Steven.

Dec 31 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.