Ali SH Ali SH - 1 year ago 89
Python Question

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)

def func1(result):
result = result ** 2
return result

def func2(result):
result = result ** 4
return result

class1 = MyClass()
class1.callback = func1
class1.callback = func2

my question is how i can use
decorator for self.callback in this code?

Answer Source

I based on this code don't see a need for properties but here it is anyway.

class MyClass:
    def __init__(self):
        self.__callback = None

    def cb(self):
        return self.__callback

    def cb(self, new_cb):
        self.__callback = new_cb
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download