class C: vs class C(object):

Matt McCredie mccredie at
Fri Jul 20 01:20:55 CEST 2007

> >How about "broke" instead of "deprecated":
> >
> >
> > >>> class Old:
> >...   def __init__(self):
> >...     self._value = 'broke'
> >...   value = property(lambda self: self._value)
> >...
> How is this broken?  Properties are not supported for old-style classes.
> They may not support features introduced in new-style classes, but that's
> hardly the same as "broken".

What does that give you that this does not:

class Old:
 def __init__(self):
  self.value = 'broke'

To further illustrate, what happens when you do this:

class Old:
 def __init__(self):
  self._value = 'broke'
 def _set_value(self, val):
  print "set called"
 def _get_value(self):
  print "get called"
  return self._value
 value = property(_get_value, _set_value)

x = Old()
print x.value
x.value = "not broke"
print x.value
print type(x.value)
print x._value

This is what happens:

>>> x = Old()
>>> print x.value
get called
>>> x.value = "not broke"
>>> print x.value
not broke
>>> print type(x.value)
<type 'str'>
>>> print x._value

Now, no exceptions were raised or anything, but with old-style classes I'm
having difficulty thinking of a scenario where they might actually be
useful. I suppose you could use it to do a calculation on instance variables
and return the result. You are probably better of using a method for that
anyway though.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the Python-list mailing list