thebjorn <[EMAIL PROTECTED]> wrote: > It would have been nice to be able to write > > class Foo(object): > @property > def expensive(self): > self.expensive = <insert expensive db call here> > return self.expensive > > but apparently I "can't set [that] attribute" :-(
You can set and access it directly in __dict__. How about something along these lines: def cachedproperty(f): name = f.__name__ def getter(self): try: return self.__dict__[name] except KeyError: res = self.__dict__[name] = f(self) return res return property(getter) class Foo(object): @cachedproperty def expensive(self): print "expensive called" return 42 >>> f = Foo() >>> f.expensive expensive called 42 >>> f.expensive 42 It is still calling the getter every time though, so not as fast as a plain attribute lookup. -- http://mail.python.org/mailman/listinfo/python-list