Static caching property

Joseph L. Casale jcasale at activenetwerx.com
Mon Mar 21 13:03:49 EDT 2016


> One solution is to use descriptor protocol on the class, which means
> using a metaclass. I'm not sure it's the best option, but it is an
> option.

I will look at that, I wonder if however I am not over complicating it:

class Foo:
    _bar = None
    @property
    def expensive(self):
        if Foo._bar is None:
            import something
            Foo._bar = something.expensive()
        return Foo._bar

Somewhat naive, but a test with if is pretty cheap...
Thanks Chris,
jlc



More information about the Python-list mailing list