I have a working decorator for running a method in a while True and it works fine on a regular function. The problem occurs when i try to decorate a function of an instance.
This is the decorator:
from threading import Thread
def run_in_while_true(f):
def decorator(break_condition=False):
def wrapper(*args, **kwargs):
while True:
if break_condition:
return
f(*args, **kwargs)
return wrapper
return decorator
class A(object):
@run_in_while_true
def print_ch(self, ch):
print ch
@run_in_while_true
def print_with_dec(ch):
print ch
print_with_dec()('f') # Call 1
# If i would want to pass a break condition i would write this
print_with_dec(1==1 and 2*2==4)('f')
a = A()
a.print_ch()('4') # Call 2
`
Call 1 runs as expected and prints f a lot. Call 2 for some reason gets the self parameter where break_condition is and becuase of that the check for break_condition is true and the function returns.
In what way do i need to change the decorator so it works with objects as well? Thanks in advance