Create properties using lambda getter and setter
If you are extending a list
, you can also use __setitem__
, like this:
class Position(list):
def __init__(self,x=0, y=0, z=0):
super(Position, self).__init__((x,y,z))
x = property(lambda self: self[0],
lambda self,value: self.__setitem__(0, value))
y = property(lambda self: self[1],
lambda self,value: self.__setitem__(1, value))
z = property(lambda self: self[2],
lambda self,value: self.__setitem__(2, value))
Your problem is that lambda's body must be an expression and assignment is a statement (a strong, deep distinction in Python). If you insist on perpetrating lambda
s you'll meet many such cases and learn the workarounds (there's usually one, though not always), such as, in this case:
name = property(lambda self: self.__name,
lambda self, value: setattr(self,
'_X__name',
self.process_value(value)))
i.e. use the built-in setattr
(which is a function and thus acceptable in a lambda
's body) rather than assignment (which is a statement and thus unacceptable in a lambda
's body).
You also need to perform the name-mangling for the dual-underscore attribute manually (changing __name
to _X__name
as you're in class X) where the attribute name is presented as a quoted string, as it must be in setattr
, as the Pyhon compiler only does the name mangling in question for suitable identifiers, not for string literals.