Re: update attribute - (newbie)
- From: Gabriel Genellina <gagsl-py@xxxxxxxxxxxx>
- Date: Wed, 20 Dec 2006 11:11:10 -0300
At Tuesday 19/12/2006 11:49, Larry Bates wrote:
> I would like to have it that when I ask for p, method _get_p is always
> called so that attribute can be updated. How can I have this
> functionality here? thanks
>
Something like this?
class A:
def __init__(self):
self.t=4
return
def __getattr__(self, name):
if name == 'p': return self.t
else: return self.__dict__[name]
__getattr__ is called *after* normal lookup has failed, so using __dict__ here is useless (and wrong, because this method should raise AttributeError but will raise KeyError instead)
A property is more convenient in this case.
--
Gabriel Genellina
Softlab SRL
__________________________________________________ Preguntá. Respondé. Descubrí. Todo lo que querías saber, y lo que ni imaginabas, está en Yahoo! Respuestas (Beta). ¡Probalo ya! http://www.yahoo.com.ar/respuestas
.
- References:
- update attribute - (newbie)
- From: Bruce
- Re: update attribute - (newbie)
- From: Larry Bates
- update attribute - (newbie)
- Prev by Date: Re: trouble getting google through urllib
- Next by Date: Re: Any easy-to-use email send module?
- Previous by thread: Re: update attribute - (newbie)
- Next by thread: FW: [Jython-users] ERROR : parsing xml in jython
- Index(es):