Re: Are all classes new-style classes in 2.4+?
- From: Steven D'Aprano <steve@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Mon, 01 Jan 2007 00:28:21 +1100
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.
.... def __init__(self):class New(object):
.... self._n = 1
.... def getter(self):
.... return "spam " * self._n
.... def setter(self, n):
.... self._n = n
.... spam = property(getter, setter, None, None)
....
'spam 'obj = New()
obj.spam
'spam spam spam 'obj.spam = 3
obj.spam
'spam spam spam spam spam spam spam 'obj.spam = 7
obj.spam
Now try with an old-style class.
.... def __init__(self):class Old:
.... self._n = 1
.... def getter(self):
.... return "spam " * self._n
.... def setter(self, n):
.... self._n = n
.... spam = property(getter, setter, None, None)
....
'spam 'obj = Old()
obj.spam
3obj.spam = 3
obj.spam
Properties should not be used with old-style classes because they just
don't work correctly.
--
Steven.
.
- References:
- Are all classes new-style classes in 2.4+?
- From: Isaac Rodriguez
- Are all classes new-style classes in 2.4+?
- Prev by Date: py2exe 0.6.6 released
- Next by Date: Re: A question about unicode() function
- Previous by thread: Re: Are all classes new-style classes in 2.4+?
- Next by thread: A question about unicode() function
- Index(es):
Relevant Pages
|