I'm trying to bind a function to an object instance. For example, I have an object and I'm trying to bind a function to the condition attribute. The thing is that I want to do that using a decorator:
class Transition(object):
def __init__(self, enter, exit, condition=None):
if not isinstance(enter, State) or not isinstance(exit, State):
raise TypeError("Parameters should be an instance of the State class")
self.enter = enter
self.exit = exit
self.condition = None
self.effect = None
def __repr__(self):
print("Going from {} to {} with condition {} and effect {}".format(self.enter, self.exit, self.condition.__name__, self.effect.__name__))
def __eq__(self, other):
return self.enter == other.enter and self.exit == other.exit
def is_enpoint(self, endpoints):
"""
:parameter --> 'endpoints' is a tuple indicating where the transition flows(from, to)
:return --> boolean
:description --> check if the given endpoints are valid
"""
return self.enter == endpoints[0] and self.exit == endpoints[1]
Then I bind a function to the object's instance.
@bind
my_condition():
return True
After this, we should have a reference to the given function if we look at the object's instance condition attribute
Edit 1:
f = Transition()
f1 = Transition()
@bind(f)
def condition1():
return True
@bind(f1)
def condition2():
return False
The f instance should have a reference to the condition1 function and the f2 instance should have a reference to the condition2 for the condition attribute