1.字典推导式实现从字典中提取子集
prices={'ACME':45,'Apple':320,'IBM':654,'HP':20}
# >200
gt200={key:value for key,value in prices.items() if value > 200}
print(gt200)
print('------------------------------')
#key to value
tech={'Apple','IBM','HP'}
techDict={key:value for key,value in prices.items() if key in tech}
print(techDict)
out:
{'Apple': 320, 'IBM': 654}
------------------------------
{'HP': 20, 'Apple': 320, 'IBM': 654}
2.装饰器decorator
def deco(func):
print('before,hello')
func()
print('after,hello')
return func
def myfunc():
print('hello~')
myfunc=deco(myfunc) #等价于@deco(语法糖)
myfunc()
myfunc()
output:
before,hello
hello~
after,hello
hello~ #原函数多调用
hello~ #第二次没有调用deco()
装饰函数只调用了一次,而且原函数多调用一次
使用内嵌包装函数确保每次装饰函数都被调用
def deco(func):
def _deco():
print('before,hello')
func()
print('after,hello')
return _deco
@deco
def myfunc():
print('hello~')
myfunc()
myfunc()
对带参数的函数进行装饰
def deco(func):
def _deco(a,b):#参数不确定:*args,**kwargs
print('before')
ret = func(a,b)
print('after')
return ret
return _deco
@deco
def myfunc(a,b):
print(' myfunc(%s,%s) called.'%(a,b))
return a+b
myfunc(1,2)
让装饰器带类参数
class locker:
def __init__(self):
print("locker.__init__() should be not called.")
@staticmethod
def acquire():
print("locker.acquire() called.(这是静态方法)")
@staticmethod
def release():
print(" locker.release() called.(不需要对象实例)")
def deco(cls):
'''cls 必须实现acquire和release静态方法'''
def _deco(func):
def __deco():
print("before %s called [%s]." % (func.__name__, cls))
cls.acquire()
try:
return func()
finally:
cls.release()
return __deco
return _deco
@deco(locker)
def myfunc():
print(" myfunc() called.")
myfunc()
myfunc()
装饰器带类参数,并分拆公共类到其他py文件中,同时演示了对一个函数应用多个装饰器
class mylocker:
def __init__(self):
print("mylocker.__init__() called.")
@staticmethod
def acquire():
print("mylocker.acquire() called.")
@staticmethod
def unlock():
print(" mylocker.unlock() called.")
class lockerex(mylocker):
@staticmethod
def acquire():
print("lockerex.acquire() called.")
@staticmethod
def unlock():
print(" lockerex.unlock() called.")
def lockhelper(cls):
'''cls 必须实现acquire和release静态方法'''
def _deco(func):
def __deco(*args, **kwargs):
print("before %s called." % func.__name__)
cls.acquire()
try:
return func(*args, **kwargs)
finally:
cls.unlock()
return __deco
return _deco
from mylocker import *
class example:
@lockhelper(mylocker)
def myfunc(self):
print(" myfunc() called.")
@lockhelper(mylocker)
@lockhelper(lockerex)
def myfunc2(self, a, b):
print(" myfunc2() called.")
return a + b
if __name__=="__main__":
a = example()
a.myfunc()
print(a.myfunc())
print(a.myfunc2(1, 2))
print(a.myfunc2(3, 4))