python 閉包與裝飾器

2021-10-25 14:59:44 字數 3766 閱讀 6178

def

my_func()

:print

('this is my func ...'

)

def

func_outer

(func)

:print

('this is outer func ...'

)def

func_inner

(*arg,

**kwargs)

:# 可以在內函式中新增新的功能

print

('this is inner func'

)return func(

)return func_inner

f = func_outer(my_func)

f()

this is outer func ...

this is inner func

this is my func .

..

def

func_outer

(func)

:print

('this is outer func ...'

)def

func_inner

(*arg,

**kwargs)

:print

('this is inner func'

)return func(

)return func_inner

@func_outer

defmy_func()

:print

('this is my func ...'

)# my_func()

d:\user\80005354\桌面\stupy>d:/python/python.exe d:/user/80005354/桌面/stupy/1-裝飾器.py

this is outer func .

..

def

func_oouter

(name)

:def

func_outer

(func)

:print

('this is outer func ...'

)def

func_inner

(*arg,

**kwargs)

:print

('my name is '

, name)

print

('this is inner func'

)return func(

)return func_inner

return func_outer

@func_oouter(name=

'bruin'

)def

my_func()

:print

('this is my func ...'

)my_func(

)

d:\user\80005354\桌面\stupy>d:/python/python.exe d:/user/80005354/桌面/stupy/1-裝飾器.py

this is outer func ...

my name is bruin

this is inner func

this is my func ...

class

speak

(object):

def__init__

(self, func)

: self.func = func

def__call__

(self,

*arg,

**kwargs)

: self.speak(

)return self.func(

*arg,

**kwargs)

defspeak

(self)

:print

('i can speak now!'

)def

people

(*arg,

**kwargs)

:print

(kwargs[

'name'],

'is dance ~ ~'

)speak = speak(people)

speak(name=

'jack'

)type

(people)

,isinstance

(people, speak)

# 結果如下:

i can speak now!

jack is dance ~

~(function,

false

)

@speak

defpeople

(*arg,

**kwargs)

:print

(kwargs[

'name'],

'is dance ~ ~'

)p = people(name=

'jack'

)type

(people)

,isinstance

(people, speak)

# 結果如下:

i can speak now!

jack is dance ~

~(__main__.speak,

true

)

可以看到在使用裝飾器之後,people已經不是原來的那個people了,此時我們可以使用functools.wraps方法保留people原有的屬性。

import functools

class

speak

(object):

def__init__

(self,

*args,

**kwargs)

:print

(args)

def__call__

(self, func)

:

@functools.wraps(func)

definner_wraps

(*arg,

**kwargs)

: self.speak(

)return func(

*arg,

**kwargs)

return inner_wraps

defspeak

(self)

:print

('i can speak now!'

)@speak(1,

2)defpeople

(*arg,

**kwargs)

:print

(kwargs[

'name'],

'is dance ~ ~'

)people(name=

'jack'

)type

(people)

,isinstance

(people, speak)

# 結果如下(1

,2)i can speak now!

jack is dance ~

~(function,

false

)

python閉包與裝飾器

首先閉包函式我的理解是,乙個函式內可以巢狀定義乙個函式,並將巢狀定義的函式返回 如果不返回執行完函式物件也就銷毀了 巢狀函式可以接收外部函式的引數,不同引數就可以變更巢狀函式的功能,返回不同功能的巢狀函式物件。以下是閉包函式簡單示例,如果inner函式不接收outer的引數x也就沒有意義了,一定要返...

Python閉包與裝飾器

一.閉包 如果乙個內嵌函式中引用了外部函式中的變數 非全域性變數 那麼該內嵌函式稱之為閉包 也就是將組成函式的語句和這些語句的執行環境打包在一起時,得到的物件 閉包滿足的三個條件 1.必須是內嵌函式 2.外層函式返回值是內嵌函式 3.內嵌函式引用外層函式變數 def funx x def funy ...

Python 閉包與裝飾器

在函式內部再定義乙個函式,並且內部這個函式用到了外邊函式的變數,那麼將內部函式以及用到的一些變數稱之為閉包。兩個函式巢狀,外層函式返回內層函式的引用,外層函式必須傳引數 外層函式不傳參相當於只定義內層函式,沒有什麼用。注意點 由於閉包引用了外部函式的區域性變數,則外部函式的區域性變數沒有及時釋放,消...