Python 裝飾器筆記

2021-09-22 17:07:15 字數 3948 閱讀 9627

def wrap_in_tag_b(fn): #

wrap_in_tag_b 是真正的裝飾器

def

return

"

" + fn() + "

"

return

defwrap_in_tag_i(fn):

def

return

"" + fn() + "

"return

@wrap_in_tag_b

@wrap_in_tag_i

defhello():

return

"hello

"print(hello()) #

returns "hello world"

def

wrap_in_tag_b(fn):

def

return

"

" + fn(arg) + "

"

return

defwrap_in_tag_i(fn):

defreturn

"" + fn(arg) + "

"return

@wrap_in_tag_b

@wrap_in_tag_i

defhello(name):

return

"hello {}

".formar(name)

print(hello('jack'))

def

wrap_in_tag(deco_arg):

#裝飾器引數:deco_arg

#可以在任意位置使用

def real_decorator(func): #

real_decorator才是即將返回的真正的裝飾器

#原函式引數:func_arg

#只可以在此位置使用

def

return

"<>

".format(deco_arg, func())

return

return

real_decorator

@wrap_in_tag('b

')@wrap_in_tag('i

')defhello():

return

"hello

"print(hello())

def

wrap_in_tag(deco_arg):

#裝飾器引數:deco_arg

#可以在任意位置使用

def real_decorator(func): #

real_decorator才是即將返回的真正的裝飾器

def

#原函式引數:func_arg

#只可以在此位置使用

return

"<>

".format(deco_arg, func(func_arg))

return

return

real_decorator

@wrap_in_tag('b

')@wrap_in_tag('i

')defhello(name):

return

"hello {}

".format(name)

print(hello('

jack

'))

class

wrap_in_tag(object):

def__init__

(self, deco_arg):

self.tag =deco_arg #裝飾器引數:deco_arg

def__call__

(self, func):

defnewf(): #原函式無引數

return

"<>

".format(self.tag, func())

return

newf

@wrap_in_tag('b

')@wrap_in_tag('i

')defhello():

return

'hello

'print(hello())

class

wrap_in_tag(object):

def__init__

(self, deco_arg):

self.tag =deco_arg#裝飾器引數:deco_arg

def__call__

(self, func):

defnewf(func_arg): #原函式引數: func_arg

return

"<>

".format(self.tag, func(func_arg))

return

newf

@wrap_in_tag('b

')@wrap_in_tag('i

')defhello(name):

return

"hello {}

".format(name)

print(hello('

jack

'))

def

wrap_in_tag(deco_arg):

def real_decorator(func):

類方法接收第乙個引數都是self,這個必須有,而無論func_arg是否有!!!

return

"<>

".format(deco_arg, func(func_arg))

return

return

real_decorator

class

foo(object):

@wrap_in_tag('b

')@wrap_in_tag('i

')defhello(self, name):

return

'hello {}".format(name)

@wrap_in_tag('b

')@wrap_in_tag('i

')defbyebye(self, name):

return

'byebye {}".format(name)

f =foo()

print(f.hello('

jack

')

class

wrap_in_tag(object):

def__init__

(self, deco_arg):

self.tag =deco_arg

def__call__

(self, func):

def newf(slf, func_arg): #

slf必須!self被占用了,那就給第乙個引數另乙個名字slf(隨便)

return

"<>

".format(self.tag, func(func_arg))

return

newf

class

foo(object):

@wrap_in_tag('b

')@wrap_in_tag('i

')defhello(self, name):

return

'hello {}".format(name)

@wrap_in_tag('b

')@wrap_in_tag('i

')defbyebye(self, name):

return

'byebye {}".format(name)

f =foo()

print(f.hello(

'jack'))

python 裝飾器筆記

python的裝飾器是乙個函式b 用來裝飾另乙個函式a 使a具有b的功能,執行a方法同時 也會執行b方法 這種用法用到 內部函式 函式傳遞 沒有使用 def debug func print 1111 內部函式返回 入參函式 並執行 return func print 2222 裝飾器函式返回 內部...

Python 裝飾器筆記

def wrap in tag b fn wrap in tag b 是真正的裝飾器 def return fn return defwrap in tag i fn def return fn return wrap in tag b wrap in tag i defhello return h...

Python 學習筆記 裝飾器

裝飾器也是乙個函式 巢狀 用來裝飾某個函式,來看下面的 import time deftime count func def start time.time func end time.time print this funnction costs end start deftellhi print...