[Python] @staticmethod with @property

Discussão em 'Python' iniciado por Stack, Abril 19, 2025.

  1. Stack

    Stack Membro Participativo

    I want

    Stats.singleton.twitter_count += 1


    and I thought I could do

    class Stats:
    singleton_object = None

    @property
    @staticmethod
    def singleton():
    if Stats.singleton_object:
    return Stats.singleton_object
    Stats.singleton_object = Stats()
    return Stats.singleton()


    But it throws an exception:

    >>> Stats.singleton.a = "b"
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    TypeError: 'property' object has only read-only attributes (assign to .a)

    Continue reading...

Compartilhe esta Página