C-style static variables in Python?
apt.shansen at gmail.invalid
Sat Apr 3 05:11:21 CEST 2010
On 2010-04-02 19:42:29 -0700, Ethan Furman said:
> Terry Reedy wrote:
>>> In<Xns9D4EC021DC8EAduncanbooth at 127.0.0.1> Duncan
>>> Booth<duncan.booth at invalid.invalid> writes:
>>>> class Spam(object):
>>>> mongo = None
>>>> def __call__(self, x, y, z):
>>>> if self.mongo is None:
>>>> self.mongo = heavy_lifting_at_runtime()
>>>> return frobnicate(x, y, z, self.mongo)
>> Unless one wants the intialization of mongo delayed in case spam is
>> never called, it can go in __init__ instead.
> As a matter of fact, I have an object that is usually not called during
> it's modules use, so I put in __getattr__. Sped the modules load time
> back up to pert near instantaneous. :)
self._mondo = None
if self._mondo is None:
self._mondo = heavy_lifting_at_runtime()
mondo = property(_get_mondo)
def __call__(self, x, y, z):
return frobnicate(x,y,z, self.mondo)
I don't know if properties are really faster or slower then a
__getattr__, but I find them a lot cleaner if I want to delay some
calculation until needed like that.
... p.s: change the ".invalid" to ".com" in email address to reply privately.
More information about the Python-list