In [2]:
# 装饰器应用
from functools import wraps
def decorator_name(f):
@wraps(f)
def decorated(*args, **kwargs):
if not can_run:
return "Function will not run"
return f(*args, **kwargs)
return decorated
@decorator_name
def func():
return ('Function is running')
can_run = True
print(func())
In [ ]:
# 授权装饰器
from functools import wraps
def requires_auth(f):
@wraps(f)
def decrated(*args, **kwargs):
auth = request.authorization
if not auth or not check_auth(auth.username, auth.password)
authenticate()
return f(*args, **kwargs)
return decrated
In [3]:
# 日志装饰器
from functools import wraps
def logit(func):
@wraps(func)
def with_loggin(*args, **kwargs):
print(func.__name__ + " was called")
return func(*args, **kwargs)
return with_loggin
@logit
def addition_func(x):
return x + x
result = addition_func(4)
In [15]:
# 在函数中嵌入装饰器
from functools import wraps
def logit(logfile='out.log'):
def logging_decorator(func):
@wraps(func)
def wrapped_function(*args, **kwargs):
log_string = func.__name__ + " was called"
print(log_string)
# 打开logfile,并写入内容
with open(logfile, 'a') as opened_file:
opened_file.write(log_string + '\n')
return wrapped_function
return logging_decorator
@logit()
def myfunc1():
pass
myfunc1()
@logit(logfile='func2.log')
def myfunc2():
pass
myfunc2()
In [18]:
# 装饰器类
class logit(object):
def __init__(self, logfile='out.log'):
self.logfile = logfile
def __call__(self, func):
log_string = func.__name__ + " was called"
print(log_string)
# 打开logfile写入
with open(self.logfile, 'a') as opened_file:
opened_file.write(log_string + '\n')
# 现在,发送一个通知
self.notify()
def notify(self):
# logit只打日志,不做别的
pass
@logit()
def func1():
pass
# 创建一个logit子类,来添加email的功能
class email_logit(logit):
'''
一个logit的实现版本, 可以在函数调用时发送email给管理员
'''
def __init__(self, email="admin@myproject.com", *args, *kwargs):
self.email = email
super(logit, self).__init__(*args, **kwargs)
def notify(self):
# 发送邮件
pass
In [19]:
def add(v1, v2):
return v1 + v2
result = add(1,2)
print(result)
def add2(v1, v2):
global result
result = v1 + v2
add2(2,3)
print(result)
In [ ]:
# 多个return值
In [24]:
# 对象变动Mutation
def add_to(num, target=[]):
target.append(num);
return target
add_to(1)
add_to(2)
add_to(3)
# def add_to2(num, target=None):
# if target is None:
# target = []
# target.append(num)
# return target
# add_to2(1)
# add_to2(2)
Out[24]:
In [ ]:
# __slots__属性
# 不使用__slots__
def MyClass(object):
def __init__(self, name, identifier):
self.name = name
self.identifier = identifier
self.set_up()
def MyClass2(object):
__slots__ = ['name', 'identifier']
def __init__(self, name, identifier):
self.name = name
self.identifier = identifier
self.set_up()
In [36]:
import ipython_memory_usage.ipython_memory_usage as imu
imu.start_watching_memory()
def MyClass(object):
def __init__(self, name, identifier):
self.name = name
self.identifier = identifier
obj = MyClass("tom")
print(obj)
num = 1024 * 256
x = [MyClass("tom") for i in range(num)]
In [40]:
def MyClass(object):
def __init__(self, name, identifier):
self.name = name
print(identifier)
self.identifier = identifier
my = MyClass("tom")
In [ ]: