[Tutor] Class Attribute "Overloading?"
Kent Johnson
kent37 at tds.net
Thu Aug 16 03:51:31 CEST 2007
Alan Gauld wrote:
> "Vincent Gulinao" <vincent.gulinao at gmail.com> wrote in message
> news:fcc3e6170708151159lf302c3er636eabb627606f23 at mail.gmail.com...
>> Sorry about that. I want something like:
>>
>> class foo:
>>
>> def __init__(self):
>> self.attr1 = None
>> def get_attr1(self):
>> if not self.attr1:
>> attr1 = <get value from DB, very expensive query>
>> self.attr1 = attr1
>> return self.attr1
>>
>> such that:
>> foo_instance = foo()
>> foo_instance.get_attr1()
>> foo_instance.attr1
>>
>> gets the same value.
>
>
> That looks like an ideal candidate for a "property".
I guess the requirements are still not clear. If there is just one
attribute to be read from the database, a property will work well. If
the OP wants to delegate many attributes, the __getattr__ approach might
be simpler.
Kent
More information about the Tutor
mailing list