迷你課程:Python-14~Decorators

Quick look

def mydecorator(function):

      def wrapper():
            print('I am a decorator')
            function()

      return wrapper
def hello_world():
      print('Hello world')

mydecorator(hello_world)()

 
def mydecorator(function):

      def wrapper():
            print('I am a decorator')
            #function()

      return wrapper
def hello_world():
      print('Hello world')

mydecorator(hello_world)()
##> only print 'I am a decorator'

因為mydecorator()是回傳wrapper 而沒有call 因此要再加()

def mydecorator(function):

      def wrapper():
            print('I am a decorator')
            function()

      return wrapper

@mydecorator
def hello_world():
      print('Hello world')

hello_word()

會得到一樣結果

def mydecorator(function):

      def wrapper():
            print('I am a decorator')
            function()

      return wrapper

@mydecorator
def hello_world(person):
      print(f'Hello {person}')

hello_word()

這樣會出錯,因為wrapper 並不允許輸入,但hello_world可以,可以這樣做

def mydecorator(function):

      def wrapper(*args, **kargs):
            print('I am a decorator')
            function(*args, **kargs)

      return wrapper

@mydecorator
def hello_world(person):
      print(f'Hello {person}')

hello_word('YHD')

實際範例

Yang-Hong Dai 戴揚紘
Yang-Hong Dai 戴揚紘
主治醫師

我的研究興趣為結合數據分析來探討各種癌症生物學。

comments powered by Disqus