I have the following code:
class Foo:
iterations = 3
class Bar(Foo):
@test_decorator(<????>)
def hello(self):
print("Hello world!")
def test_decorator(input):
def my_decorator(func):
def wrapper(*args, **kwargs):
print("Something is happening before the function is called.")
for _ in range(input):
func(*args, **kwargs)
print("Something is happening after the function is called.")
return wrapper
return my_decorator
I would like to pass my iterations variable which is in the parent class to the decorator test_decorator which is in my child class, instead of <????>
I tried the following ways:
self.iterationdoesn't work since we don't have access toselfFoo.iterationsdoesn't work because it will act as a constant, if we changeiterations"hello world" will be displayed only 3 times instead of 5 (as in the example below)
Example:
b = Bar()
b.iterations = 5
b.hello()
# "hello world" will be displayed 3 times
Is there a way to do this or is it anti pattern to python ?
Foo.iterationsdoes, I do not think that specific pattern is the solution.