Issue
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.iteration
doesn't work since we don't have access toself
Foo.iterations
doesn'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 ?
Solution
I found a solution to your problem. The idea is to write your own decorator.
Since that decorator is a function wrapping your method, it has access to the class instance using the *args
first index. From there, you can access the iterations
variable:
def decorator(iterations, source):
def inner_transition(func):
return func
return inner_transition
def custom_transition(source):
def inner_custom_transition(func):
def wrapper(*args, **kwargs):
iterations = args[0].iterations
return decorator(iterations=iterations, source=source)(func)(*args, **kwargs)
return wrapper
return inner_custom_transition
class Foo:
iterations = 3
class Bar(Foo):
@custom_transition(source="my_source")
def hello(self, string_to_show, additional_string = "default"):
print(string_to_show)
print(additional_string)
bar = Bar()
bar.hello("hello", additional_string="++++")
Result:
hello
++++
Answered By - Dynamia
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.