[英]passing function to a class using @property.setter decorator
I am making a class that i want to declare a variable which hold a function in it and i want to call them after i do some processing on some information.but i don't know how to use property decorator in this situation. 我正在创建一个类,我想声明一个在其中包含函数的变量,并且在对某些信息进行一些处理之后我想调用它们。但是我不知道在这种情况下如何使用属性装饰器。 i already have this code:
我已经有此代码:
class MyClass:
def __init__(self):
self.callback = None
def run():
#do something here...
result = self.callback(result)
print(result)
def func1(result):
result = result ** 2
return result
def func2(result):
result = result ** 4
return result
class1 = MyClass()
class1.callback = func1
class1.run()
class1.callback = func2
class1.run()
my question is how i can use @property
and @property.setter
and @property.getter
decorator for self.callback in this code? 我的问题是如何使用
@property
和@property.setter
和@property.getter
在此代码装饰商self.callback?
I based on this code don't see a need for properties
but here it is anyway. 基于此代码,我看不到需要
properties
但是无论如何这里都是如此。
class MyClass:
def __init__(self):
self.__callback = None
@property
def cb(self):
return self.__callback
@cb.setter
def cb(self, new_cb):
self.__callback = new_cb
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.