如何使我的模式观察者与元类一起工作?

问题描述 投票:0回答:1

我想在我的类属性上设置模式观察者

我尝试使用@classmethod,但没有setter属性。

class dataframe():
    df = None
    @classmethod
    @property
    def weather(cls):
        return cls.df
    @classmethod
    @weather.setter
    def weather(cls,value):
        cls.df= value
        print("the weath was change {}".format(cls.df))
<ipython-input-119-7e26ac08cb26> in dataframe()
      6         return cls.df
      7     @classmethod
----> 8     @weather.setter
      9     def weather(cls,value):
     10         cls.df= value

AttributeError: 'classmethod' object has no attribute 'setter'

然后,我尝试使在那里找到的解决方案适合我的问题Using property() on classmethods

class dataframe_meta(type):
    def __init__(cls, *args, **kwargs):
        cls.df = None

    @property
    def change(cls):
        return cls.df

    @change.setter
    def change(cls, value):
        cls.df = value
        print("the weath was change {}".format(cls.df))

class dataframe(metaclass=dataframe_meta):
    pass

dataframe.df = 5它不返回任何错误,但是未显示功能设置器中的print

如何使其正常工作?

python observer-pattern metaclass
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.