0
def timeit(method):
    def timed(*args, **kw):
        ts = time.time()
        result = method(*args, **kw)
        te = time.time()
    return timed


class Base:
    @timeit
    def run(self):
        print "Base running"

    def get_statistics(self):
        print("Stats"+str(self)) 

class Derived1(Base):
    def run(self):
        print("Derived 1 running")


class Derived2(Base):
    def run(self):
        print("Derived 2 running")

I want to calculate the time taken by run method of derived class just by writing some piece of code in Base class.

  • 1
    I don't know that you can do what you're wanting. [This](https://stackoverflow.com/questions/3001138/python-decorators-and-inheritance) is the closest I could find. – gnodab Apr 21 '20 at 16:48
  • Note that when you override a method in the derived class it is replaced. No more relation with the base decorated method, unless you call the base method inside. The comment above provides a good answer. Basically you define the decorator function inside the class and use it on any method you like. The decorator defined as a static method is like one outside the class, but is now in the class namespace. – progmatico Apr 24 '20 at 19:10
  • The simpler answer is a question. Why don't you simply decorate again the method with the external decorator? A timing decorator does not look something tied to a particular class. – progmatico Apr 24 '20 at 19:11

0 Answers0