Python || 進階用法 - SeanWu1977/Machine-Learning GitHub Wiki

Decorator

# decorator 即先執行外層函數,在執行內層函數 
# my_wrap會自動執行, 因為有return my_wrap
# Star 也會執行,因為out就會呼
def ChrismasTree(func):
    def my_wrap(*args, **kwargs):
        print("pos 1 ....")
        print("pos 2 ....")
        out = func(*args, **kwargs)
        print("pos 3 ....")
        print("my name is: ", func.__name__)
    return my_wrap

@ChrismasTree
def Star(color):
    # with color do something ...
    print(color)
    return

>>> Star("red")  # Star = ChrismasTree(Star)

#decorator指標 --> function(fun)
def decorator(fun):
    def my_wrap(*args, **kwargs):
        print('decorator' )
        fun(*args, **kwargs)
    return my_wrap


#funcA指標 --> function(name)
@decorator
def funcA(name):
    print('function A {}'.format(name))
 
# decorator --> function(funcA指標)  
# return my_wrap指標
# my_wrap指標 -> function(*args, **kwargs):
        #        print('decorator' )
        #        funcA指標 (*args, **kwargs)

# funcA指標  --> function(*args, **kwargs):
        #        print('decorator' )
        #        funcA(*args, **kwargs)

funcA('Sean2')  #  my_wrap('Sean2') 
                #  只是在執行fun(*args, **kwargs)
                #  會執行fun指標(即funcA)實際指的位置

# 類別中用decorator傳self
    def LoginRequire(func):
        # *args[0] --> self
        def in_wrap(*args, **kwargs):
            args[0].statusbarmsg(args[0])
            login = args[0]._login
            if login:
                out = func(args[0])
            else:
                args[0].statusbarmsg('E','請先登入系統')

        return in_wrap    

    @LoginRequire
    def ActionSearch(self):
        print('search')

    def statusbarmsg(self, type='N', msg=''):
        if type == 'E':
            self.statusbar.setStyleSheet("background-color: red");
        else:
            self.statusbar.setStyleSheet("background-color: ");
        
        self.statusbar.showMessage(msg)   

Dispatch

# 利用getattr抓取要執行的function
# 並於return時執行(只要加()就會執行,如不加則會傳會function)
# 傳入參數用**kwargs 或 *args

class Main():
    
    def a(self, *args, **kwargs):
        str = 'run function a. '
        if len(args) > 0 :
            for l in args:
                str += l + ','
        if len(kwargs) > 0 :
            print(kwargs)
            for k,v in kwargs.items(): 
                str += "k:{}, v:{} ".format(k,v)
        print(str)
    
    def b(self, *args, **kwargs):
        print('run function b' + args[0])
    
    def error(self, *args, **kwargs):
        print('run function other')
    
    def dispatch(self, fun, *args, **kwargs):
        _fun = getattr(self, fun, self.error)
        return _fun(*args, **kwargs)


a=Main()
a.dispatch('a')
a.dispatch('d')
a.dispatch('a','l1','l2')
a.dispatch('a', a='asdf',b='4532')


run function a. 
run function other
run function a. l1,l2,
{'a': 'asdf', 'b': '4532'}
run function a. k:a, v:asdf k:b, v:4532 

class / static method

class Father():
    @staticmethod
    def hello(name):
        print('hello!')
    @classmethod
    def sayhello(cls, name):
        if cls.__name__ == 'Son':
            print('Hi boy' + name)
        if cls.__name__ == 'Daughter':
            print('Hi girl' + name) 

class Son(Father):
    pass

class Daughter(Father):
    pass
        
Son.sayhello('sean')
Daughter.sayhello('marry')

>>> Hi boy sean
>>> Hi girl marry

class FF:
    @staticmethod
    def ma(x,y):
        return x*y
    @classmethod
    def rf(cls):
        return cls.ma

a = FF.rf()
print(a(5,6))