Re: update attribute - (newbie)



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

.