properties and get/set methods
Dan Bishop
danb_83 at yahoo.com
Sat Apr 5 23:16:08 EST 2003
Timothy Grant <tjg at craigelachie.org> wrote in message news:<mailman.1049581810.25706.python-list at python.org>...
> Hi all,
>
> Given the following class...
>
> class monty(object):
> def __init__(self):
> self.__foo = 0
> self.__bar = 0
>
> def setvar(self):
> do_some_attribute_generic_thing()
> set_appropriate_attribute()
>
> def getvar(self):
> do_some_attribute_generic_thing()
> return appropriate_attribute()
>
> foo = property(getvar, setvar)
> bar = property(getvar, setvar)
>
> =====================================
>
> Is it possible to determine which attribute was used to as the trigger for the
> call to setvar() or getvar()? so that set_appropriate_attribute() or
> appropriate_attribute() can set the correct attribute or get the correct
> attribute?
Why not just use two different getvar and setvar functions?
However, if you absolutely must insist on using only one getter/setter
pair, you could use
class monty(object):
def __init__(self):
self.__foo = 0
self.__bar = 0
def setvar(self, value, whatCalledIt=None):
do_some_attribute_generic_thing()
set_appropriate_attribute()
def getvar(self, whatCalledIt=None):
do_some_attribute_generic_thing()
return appropriate_attribute()
foo = property(lambda self: self.getvar('foo'),
lambda self, value: self.setvar(value, 'foo'))
bar = property(lambda self: self.getvar('bar'),
lambda self, value: self.setvar(value, 'bar'))
More information about the Python-list
mailing list