class C: vs class C(object):

Matt McCredie mccredie at gmail.com
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
broke
>>> x.value = "not broke"
>>> print x.value
not broke
>>> print type(x.value)
<type 'str'>
>>> print x._value
broke

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.

Matt
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20070719/921754ca/attachment.html>


More information about the Python-list mailing list