[Tutor] Class Attribute "Overloading?"
Vincent Gulinao
vincent.gulinao at gmail.com
Wed Aug 15 20:59:20 CEST 2007
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()
then:
foo_instance.get_attr1()
and
foo_instance.attr1
gets the same value.
Such that you get to derive attr1 only as needed and just once, both outside
and within foo class.
Or is it a bad idea or just plain ugly to do something like that? If it is,
kindly suggest better approach.
Thanks.
On 8/16/07, bob gailer <bgailer at alum.rpi.edu> wrote:
>
> Vincent Gulinao wrote:
> >
> > Sorry, I just started experimenting on Python classes...
> >
> > Is there any way a reference to a class attribute
> > ([class].[attribute]) be treated like a method ([class].[method]())?
> >
> Attributes are objects. A method is an object. If you want to know
> something more specific, please rephrase the question.
>
> I for one don't really know what you want.
> >
> > I have a class with DB component. Some of its attributes are derived
> > from DB and I find it impractical to derive all at once upon __init__.
> >
> > Of course we can use dictionary-like syntax ([class]["[attribute]"])
> > and use __getitem__, checking for existence and None of the attribute
> > before deriving the value from the DB. But I think [class].[attribute]
> > is more conventional (Is it?). Or someone suggest a more Pythonic way.
> >
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20070816/03f8e9ae/attachment-0001.html
More information about the Tutor
mailing list