日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

python进阶-Python 进阶用法 (持续更新)

發(fā)布時(shí)間:2024/7/23 python 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python进阶-Python 进阶用法 (持续更新) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

裝飾器(Decorator)

Python 的裝飾器是任何可調(diào)用對(duì)象(callable object),用于修改函數(shù)(Function)或類(Class)。按照用途可分為:

函數(shù)裝飾器

類裝飾器

裝飾器的接口定義可概括為:

接收某個(gè)函數(shù)或類的引用作為參數(shù);

修改該函數(shù)或類并返回。

簡(jiǎn)單函數(shù)裝飾器示例

理解裝飾器

def my_decorator(func):

def wrapped_func(arg):

print("Before calling " + func.__name__)

func(arg)

print("After calling " + func.__name__)

return wrapped_func

def foo(arg):

print("Hi, foo has been called with " + str(arg) )

print("We call foo BEFORE decoration:")

foo("no decoration"); print()

print("We NOW decorate foo with my_decorator... ")

foo = my_decorator(foo)

print("We call foo AFTER decoration:")

foo("decoration")

程序?qū)?yīng)的輸出為:

We call foo BEFORE decoration:

Hi, foo has been called with no decoration

We NOW decorate foo with my_decorator...

We call foo AFTER decoration:

Before calling foo

Hi, foo has been called with decoration

After calling foo

上述例子中的用法在實(shí)際開發(fā)中并不常用,我們更傾向于下面的寫法。

語法糖寫法

Python 提供一種更簡(jiǎn)潔、直觀的裝飾器寫法。例如我們可以把上述例子寫成更簡(jiǎn)便的形式:

def my_decorator(func):

def wrapped_func(arg):

print("Before calling " + func.__name__)

func(arg)

print("After calling " + func.__name__)

return wrapped_func

@my_decorator

def foo(arg):

print("Hi, foo has been called with " + str(arg) )

foo("decoration")

函數(shù)裝飾器應(yīng)用示例

統(tǒng)計(jì)函數(shù)調(diào)用次數(shù)

def call_counter(func):

def func_with_counts(arg):

func_with_counts.calls += 1

return func(arg)

func_with_counts.calls = 0

return wrapped_func

@call_counter

def foo(arg):

return "foo: " + str(arg)

print("foo has been called {} time(s)".format(foo.calls))

for i in range(0, 51, 10):

foo(i)

print("foo has been called {} time(s)".format(foo.calls))

程序?qū)?yīng)的輸出結(jié)果是:

foo has been calld 0 time(s)

foo: 0

foo: 10

foo: 20

foo: 30

foo: 40

foo: 50

foo has been calld 6 time(s)

用記憶表(Memoization1)優(yōu)化 Fibonacci 數(shù)列算法

def memoize(func):

memo = {}

def memoized_func(arg):

if arg not in memo:

memo[arg] = func(arg)

return memo

return memoized_func

@memoize

def fib(n):

if n == 0:

return 0

elif n = 1:

return 1

else:

return fib(n-1) + fib(n-2)

for i in range(20):

print(fib(i), end=", ")

print(fib(20))

程序?qū)?yīng)的輸出結(jié)果是:

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765

類裝飾器

首先需要明確,Python 的裝飾器是任何可調(diào)用對(duì)象而不僅限于函數(shù)。我們可以定義一個(gè)類并使其對(duì)象可調(diào)用。例如我們可以定義一個(gè)帶緩存功能的類計(jì)算 Fibonacci:

class Fibonacci:

def __init__(self):

self.cache = {}

def __call__(self, n):

if n not in self.cache:

if n == 0:

self.cache[0] = 0

elif n == 1:

self.cache[1] = 1

else:

self.cache[n] = self.cache[n-1] + self.cache[n-2]

return cache[n]

fib = Fibonacci()

for i in range(20):

print(fib(i), end=", ")

print(fib(20))

程序?qū)?yīng)的輸出結(jié)果是:

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765

因此,我們可以構(gòu)建一個(gè)可調(diào)用對(duì)象并令其作為裝飾器。

在下面的例子中,我們用類裝飾器統(tǒng)計(jì)函數(shù)調(diào)用次數(shù):

class CallCounter:

def __init__(self, func):

self.calls = 0

self.func = func

def __call__(self, arg):

self.calls += 1

return self.func(arg)

@CallCounter

def foo(arg):

return "foo: " + str(arg)

print("foo has been called {} time(s)".format(foo.calls))

for i in range(0, 51, 10):

foo(i)

print("foo has been called {} time(s)".format(foo.calls))

注意 Memoization 是專業(yè)術(shù)語,不是 Memorization。?

原文地址:https://www.cnblogs.com/LexLuc/p/10259110.html

總結(jié)

以上是生活随笔為你收集整理的python进阶-Python 进阶用法 (持续更新)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。