Properties vs. get/set-methods

Michael 'Mickey' Lauer mickey at tm.informatik.uni-frankfurt.de
Fri Aug 23 02:06:07 CEST 2002


Mark McEahern <marklists at mceahern.com> wrote:
>> When using properties, I can't use val to save the state.
>> Accessing self.val would lead to infinite recursion here. So, if I
>> want to save the state, I'd have to introduce another attribute.
> 
> The answer is embodied in this sample code:
> 
> class foo(object):
> 
>  def __init__(self):
>    self._val = None
> 
>  def setVal(self, val):
>    self._val = val
> 
>  def getVal(self):
>    return self._val
> 
>  val = property(getVal, setVal)
> 
> Where's the infinite recursion in this?

Hmm... sure, I already stated that involving a second attribute
is needed to save state. My original question about the real
usage scenario for properties remains though.

:M:



More information about the Python-list mailing list