Computed static property in python
<p>Is it possible to have a static property on a class that would be computed as a one off. The idea would be to be able to do it like so:</p>

<pre><code>class Foo:
    static_prop = Foo.one_off_static_method()

    @staticmethod
    def one_off_static_method():
        return 'bar'
</code></pre>

<p>I thought of using <code>__new__</code> as well.</p>

<pre><code>Class Foo:
    def __new__(cls):
         cls.static_prop = ... do everything here
</code></pre>

<p>Not sure the implications of that though.</p>

<p>Here you go, I made a small descriptor for you :-)</p>

<p>Upon accessing the attribute, it will be computed and cached.</p>

<pre><code>class CachedStaticProperty:
    """Works like @property and @staticmethod combined"""

    def __init__(self, func):
        self.func = func

    def __get__(self, inst, owner):
        result = self.func()
        setattr(owner, self.func.__name__, result)
        return result
</code></pre>

<p>The way it works is rather simple:</p>

<ol>
<li>Upon using the decorator syntax, I save the function internally.</li>
<li>Upon access, I call the function and set the value as the class value with the same name as the original function.</li>
</ol>

<p>That's all there is to it. Simple and efficient.</p>

python static-methods