Looking for assignement operator
Bruno Desthuilliers
onurb at xiludom.gro
Tue Oct 17 10:51:31 EDT 2006
Jerry wrote:
(snip)
> I believe the property function is what you are looking for.
It is not.
> e.g.
>
> class MyClass:
Descriptors don't work fine with old-style classes. Should be:
class MyClass(object):
> def __init__(self, val):
> self.setval(val)
>
> def getval(self):
> return self._val
>
> def setval(self, val):
> assert(isinstance(val, int))
> self._val = val
>
> _val = property(self.getval, self.setval)
NameError : self is not defined.
Should be :
_val = property(getval, setval)
but then - since setval() now calls _vals.__set__(), which itself calls
setval(), you have a nice infinite recursion (well, almost infinite -
hopefully, Python takes care of it).
May I kindly suggest that you learn more about properties and test your
code before posting ?-)
Anyway, even with the following correct code, this won't solve the OP's
question:
class MyClass(object):
def __init__(self, val):
self.val = val
def _getval(self):
return self._val
def _setval(self, val):
self._val = int(val)
val = property(_getval, _setval)
m = MyClass(42)
m
=> <__main__.MyClass object at 0x2ae5eaa00410>
m.val
=> 42
m = 42
m
=> 42
type(m)
=> <type 'int'>
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'onurb at xiludom.gro'.split('@')])"
More information about the Python-list
mailing list