Re: Are all classes new-style classes in 2.4+?



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.

.



Relevant Pages

  • trouble with generators
    ... I'm stuck in a maze of new style classes and generators. ... def records: ... def display(self, rec): ... I expected to get 3 different class objects from both sections, ...
    (comp.lang.python)
  • Re: How to give a global variable to a function which is in a module?
    ... Kurda Yon wrote: ... I would like to declare a global variable, ... def test: ... 'Global' means 'global to the current module in which the global statement appears. ...
    (comp.lang.python)
  • very noob question about class variables
    ... simplify this I dont post the program but I post a example ... Do I need declare this like class variable necessarily?..inside my def ...
    (comp.lang.ruby)
  • Re: Property in derived class
    ... One way is to have the property refer to a proxy that performs the ... Another way is to declare properties using something like the ... fget = getattr ... def func: return "foo" ...
    (comp.lang.python)
  • Re: very noob question about class variables
    ...    puts var ... Do I need declare this like class variable necessarily?..inside my def ... Class variables are simply variables that you can access from any ...
    (comp.lang.ruby)