The external function of the Python class decorates the inner function of the class

The

code is implemented by instantiating the object to call the class method. Now there is a new requirement. Without changing the business code, I want to add a decorator to execute the decorator function when calling the specific class method

Code example:

def decorater(func):
    print("This function"s name is %s" % func.__name__)
    def wrapper(*args,**kargs):
        return func()
    return wrapper

@decorater
class NEW():
    def showTime(self):
        print(time.strftime("%Y-%m-%d %H:%M:%S",time.localtime()))    

    def showName(self):
        print("My name is Lee.")

NEW().showName()
NEW().showTime()

the above code is an example of a function decorating class that executes decorater, when the NEW object is instantiated. My goal is whether decorater? can be executed only when showName is called. Instead of executing it every time it is instantiated. Ask the gods who think no for help, thank you

Dec.15,2021

-sharp coding:utf-8
import time


def decorater(cls):

    showName = cls.showName  -sharp 

    def wrapper(*args, **kwargs):
        print("This function's name is %s" % cls.__name__)
        return showName(*args, **kwargs)
    cls.showName = wrapper

    return cls


@decorater
class NEW():
    def showTime(self):
        print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))

    def showName(self):
        print("My name is Lee.")


instance = NEW()
print("instanced\n")
instance.showTime()
print("time showed\n")
instance.showName()
print("name showed")

//instanced

//2018-11-26 16:53:40
//time showed

//This function's name is NEW
//My name is Lee.
//name showed

changed it a little bit, but it's not very clear what you want to do, so it may not exactly match the business.

Menu