At a little bit of a loss here. I'm attempting to decorate all methods of a class, and using the solution here. I feel like I have all the pieces (see below) yet when I initialize the class/call its methods, nothing happens.
As a toy example, I've got the function decorator
def my_decorator(func):
def wrapper(*args):
print("Something is happening before the function is called.")
return func(*args)
print("Something is happening after the function is called.")
return wrapper
and the class decorator
def for_all_methods(decorator):
import inspect
def decorate(cls):
for name, fn in inspect.getmembers(cls, inspect.ismethod):
print(name, fn)
setattr(cls, name, decorator(fn))
return cls
return decorate
and the toy class
@for_all_methods(my_decorator)
class Car:
def __init__(self):
self.wheels = 4
self.price=20000
self.mileage = 0
def drive(self, miles):
self.mileage += miles
def depreciate(self):
self.price-=0.1*self.mileage
yet when I initialize the class
c = Car()
or call its methods, they aren't decorated. What gives? I feel like I must be missing something trivial.