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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python高级编程装饰器_Python装饰器

發布時間:2023/12/10 python 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python高级编程装饰器_Python装饰器 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

def my_decorator(function):

def _my_decorator(*args, **kw):

#在調用實際函數之前做些填充工作

res = function(*args, **kw)

#做完某些填充工作之后

return res

#返回子函數

return _my_decorator

當裝飾器需要參數時,必須使用第二級封裝。

def my_decorator(arg1, arg2):

def _my_decorator(function):

def __my_decorator(*args, **kw):

res = function()

return res

return __my_decorator

return _my_decorator

引用

因為裝飾器在模塊第一次被讀取時由解釋程序裝入,所以它們的使用必須受限于總體上可以應用的封裝器。如果裝飾器與方法的類或所增強的函數簽名綁定,它應該被重構為常規的可調用對象,從而避免復雜性。在任何情況下,當裝飾器處理API時,一個好的方法是將它們聚集在一個易于維護的模塊中。

參數檢查:

def check_param_isvalid():

def check(method):

def check_param(*args,**kwargs):

for a in args:

assert isinstance(a, int),"arg %r does not match %s" % (a,int)

assert a > 100000,"arg %r must gt 100000" % a

return method(*args, **kwargs)

return check_param

return check

@check_param_isvalid()

def foo(*args):

print args

foo(200000,500000)

緩存:

import time

import hashlib

import pickle

cache = {}

def is_obsolete(entry, duration):

return time.time() - entry['time'] > duration

def computer_key(function, args, kw):

key = pickle.dumps((function.func_name, args, kw))

return hashlib.sha1(key).hexdigest()

def memoize(duration=30):

def _memoize(function):

def __memoize(*args, **kw):

key = computer_key(function, args, kw)

if key in cache and not is_obsolete(cache[key], duration):

print 'wo got a winner'

return cache[key]['value']

result = function(*args, **kw)

cache[key] = {'value':result,'time':time.time()}

return result

return __memoize

return _memoize

@memoize()

def very_complex_stuff(a,b):

return a + b

print very_complex_stuff(2,2)

代理:

class User(object):

def __init__(self, roles):

self.roles = roles

class Unauthorized(Exception):

pass

def protect(role):

def _protect(function):

def __protect(*args, **kw):

user = globals().get('user')

if user is None or role not in user.roles:

raise Unauthorized("I won't tell you")

return function(*args, **kw)

return __protect

return _protect

tarek = User(('admin', 'user'))

bill = User(('user',))

class MySecrets(object):

@protect('admin')

def waffle_recipe(self):

print 'use tons of butter!'

these_are = MySecrets()

user = tarek

these_are.waffle_recipe()

user = bill

these_are.waffle_recipe()

上下文提供者:

from threading import RLock

lock = RLock()

def synchronized(function):

def _synchronized(*args, **kw):

lock.acquire()

try:

return function(*args, **kw)

finally:

lock.release()

return _synchronized

@synchronized

def thread_safe():

print 'haha'

thread_safe()

補充:http://2057.iteye.com/blog/1838398

參考資料:

Python高級編程

分享到:

2013-10-15 22:59

瀏覽 1214

評論

總結

以上是生活随笔為你收集整理的python高级编程装饰器_Python装饰器的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。