PHLK 生成器 裝飾器

2021-09-24 14:23:20 字數 4784 閱讀 6068

def

printf

(nums)

:for num in nums:

print

('num is %d'

%num)

yield num

defadder

(nums)

:for num in nums:

print

('adding %d'

%num)

if num %2==

0:yield num +

1else

:yield num +

2

nums =[1

,2,3

,4,5

,6,7

,8]

result = adder(printf(nums)

)

next

(result)

num is 1

adding 1

3

def

fab(n)

: i, a, b =0,

0,1while i <= n:

yield b

i = i+

1 a, b = b, a+b

for i in fab(10)

:print

(i)

112

35813

2134

5589

def

psychologlist()

:print

('please tell me you problems'

)while

true

: answer =

(yield

)if answer is

notnone

:if answer.endswith(

'?')

:print

('do not ask your self to much questions'

)elif

'good'

in answer:

print

("ahh , that's good , go on"

)elif

'bad'

in answer:

print

("don't be so negative"

)

free = psychologlist(

)next

(free)

please tell me you problems
free.send(

'what the hell?'

)

do not ask your self to much questions
free.send(

'today is ****ing bad day'

)

don't be so negative
free.send(

'today is ****ing good day'

)

ahh , that's good , go on
class

withoutdecorators

:def

some_static_method()

:print

('this is a static method'

)# 類的靜態方法

some_static_method =

staticmethod

(some_static_method)

defsome_class_method

(cls)

:print

('this is as class method'

) some_class_method =

classmethod

(some_class_method)

class

withdecorators

: @staticmethod

defsome_static_method()

:print

('this is a static method'

) @classmethod

defsome_class_method()

:print

('this is a class method'

)

def

mydecorator

(function)

:def

(*args,

**kwargs)

:# 在呼叫原始函式之前,做些什麼

result = function(

*args,

**kwargs)

# 函式呼叫之後,做些什麼

# 返回結果

return result

# 返回裝飾後的函式

非引數化裝飾器類
class

decoratorasclass

:def

__init__

(self, function)

: self.function = function

def__call__

(self,

*args,

**kwargs)

:# 呼叫原始函式前

result = self.function(

*args,

**kwargs)

# 呼叫函式之後

# 返回結果

return result

引數化裝飾器, 需要用到第二層包裝
def

repeat

(num=3)

:# num 裝飾器引數

defactual_decorator

(function)

:def

(*args,

**kwargs)

: result =

none

for _ in

range

(num)

: result = function(

*args,

**kwargs)

return result

return actual_decorator

@repeat(3)

# 帶引數的裝飾器,即便是預設引數也要帶()

deffoo()

:print

('today is ****ing bad day'

)

foo(

)>>

>today is ****ing bad day

>>

>today is ****ing bad day

>>

>today is ****ing bad day

裝飾器的內省情況:

使用裝飾器時不儲存函式的元資料(函式的注釋文件和原始函式名),裝飾器建立了乙個新函式,並返回它,完全沒有考慮被裝飾函式的標識,所以在除錯被裝飾過的函式時會很麻煩,不能訪問原始的文件字串和函式簽名。

def

dummy_decorator

(function)

:def

(*args,

**kwargs)

:'''包裝函式的內部文件'''

return function(

*args,

**kwargs)

@dummy_decorator

deffunction_a()

:'''被裝飾函式的內部文件'''

pass

>>

>function_a.__name__

>>

>

>>

>function_a.__doc__

>>

>

'包裝函式的文件'

如何解決裝飾器的內省問題
from functools import wraps

defpreserving_decorator

(function)

: @wraps(function)

def(

*args,

**kwargs)

:return function(

*args,

**kwargs)

@preserving_decorator

deffunction_b()

:'''被裝飾函式的內部文件'''

pass

>>

>function_b.__name__

>>

>

'function_b'

>>

>function_b.__doc__

>>

>

'被裝飾函式的內部文件'

裝飾器 生成器

python生成器 迭代器 裝飾器 最簡單的生成器 g x x for x in range 10 for i in g print i函式方法實現稍複雜的生成器 def fib max n,a,b 0,1,1 while n在迴圈過程中不斷呼叫yield,就會不斷中斷,通常基本不用next 來呼叫...

裝飾器 生成器

裝飾器主要用於程式功能的一些擴充套件。由於在python中,函式名 不帶括號 也是一種變數名,可以像賦值一樣給另外乙個變數。這就導致了裝飾器這種玩法。其實函式名就是儲存著函式的位址,因此,可以把這個位址賦值給另外乙個變數,與c語言中的指標基本一樣,同時與python列表的引用也是相像的。不附加新引數...

裝飾器,生成器,迭代器

裝飾器 import time def show time func def inner x start time time.time func x end time time.time print end time start time return inner show time def add...