Perl vs. Python question...
Paul Lussier
p.lussier at comcast.net
Tue Jul 14 14:25:36 EDT 2009
Lloyd Kvam <python at venix.com> writes:
> If the value will be computed on demand, __getattr__ is one way to go.
> def __getattr__(self, attr):
> if attr == 'foo':
> return self.compute_foo()
> elif ....
> else:
> raise AttributeError( attr + ' is invalid name')
>
> This is the 'traditional' approach. __getattr__ is *only* called when
> an attribute is not found. If you wanted to save the computed value, so
> that __getattr__ was no longer used:
> self.__dict__['foo'] = self.compute_foo()
> return self.foo
>
> HTH
Very much so!
--
Thanks,
Paul
More information about the gnhlug-discuss
mailing list