Just for example:
class Foo:
def wrapper(func):
def wrapped(self):
try:
return func(self)
except Exception:
raise
finally:
print('Some error in wrapped function')
return wrapped
@wrapper
def division(self, x, y):
return x / y
class Bar(Foo):
@wrapper
def multiplication(self, x, y):
return x * y
f = Foo()
print(f.division(5, 2))
b = Bar()
print(b.multiplication(3, 3))
Error:
Traceback (most recent call last):
File "d:\Dev\abc-python\count.py", line 18, in <module>
class Bar(Foo):
File "d:\Dev\abc-python\count.py", line 20, in Bar
@wrapper
NameError: name 'wrapper' is not defined
When inheriting, I expect that the child class will have all the methods of the parent, I did not find the reverse information, at least I see no reason why the wrapper is not inherited by the child class.
Yes, I can move the decorators into a separate file and import them, maybe it will be "more correct", but I want to understand why they are not inherited.