Python学习之——装饰器 Python学习之——装饰器

参考

python装饰器的4种类型:函数装饰函数、函数装饰类、类装饰函数、类装饰类
9.4 定义一个带参数的装饰器

基础

闭包概念

Python闭包(Closure)详解

闭包概念:在一个内部函数中,对外部作用域的变量进行引用,并且外部函数的返回值为内部函数,那么内部函数就叫做闭包。

示例:

def outer_func(year):
 def inner_func(month):
 print(f'year:{year}, month:{month}')
 return inner_func
closure_func = outer_func('2023')
closure_func (12)

调用func时,产生了闭包inner_func,其持有外层函数的自由变量year,当函数func的生命周期结束之后,year这个变量依然存在,因为它被闭包引用了,不会被回收。

闭包的特点:
内部函数可以读取外层函数内的局部变量,并让其常驻内存,不会被回收,所以注意内存泄漏

装饰器

一文搞懂什么是Python装饰器

装饰器是用来增强函数/类功能的一个函数

import logging
def no_arg_decorator(func):
 call_count = 0
 def inner_func(*arg, **kwargs):
 # do somethings, example
 nonlocal call_count
 call_count += 1
 print(f'call {func.__name__} func, total call count:{call_count }')
 func(*arg, **kwargs)
 return inner_func
def arg_decorator(*outer_arg, **outer_kwargs):
 def inner_func(func):
 def inner_func2(*arg, **kwargs):
 # do somethings use outer_arg, outer_kwargs, example
 file_path = outer_kwargs.get('log_file_path')
 if file_path:
 logging.basicConfig(filename=file_path,
 format='%(asctime)s - %(name)s - %(levelname)s - %(message)s-%(funcName)s',
 level=logging.DEBUG)
 logging.debug(f'debug, call {func.__name__}, arg:{arg}, kwargs:{kwargs}')
 func(*arg, **kwargs)
 return inner_func2
 return inner_func
def test_func1(a=1):
 pass
# 1.不用语法糖@符号​​​​​​​
# 1.1 无参装饰器
wraped_func1 = no_arg_decorator(test_func1)
# 1.2 有参装饰器
wraped_func2 = arg_decorator(log_file_path='log.txt')(test_func1)
# 2.采用语法糖@符号​​​​​​​
# 2.1 无参装饰器
@no_arg_decorator
def test_func2(a=1):
 pass
# 2.2 有参装饰器
@arg_decorator(log_file_path='log.txt')
def test_func3(a=1):
 pass
if __name__ == '__main__':
 print('=========================')
 wraped_func1()
 wraped_func1()
 wraped_func2(a=11)
 test_func2()
 test_func3(a=111)
# 结果
=========================
call test_func1 func, total call count:1
call test_func1 func, total call count:2
call test_func2 func, total call count:1

系统自带的装饰器

@property

Python 中 property() 函数及 @property 装饰器

class Food(object):
	def __init__(self):
	self._price = 0
 @property
 def price(self):
 return self._price 
 @price.setter
 def price(self, value):
 if value < 0:
 raise ValueError('price must large than 0!')
 self._price = value
 
 @price.deleter
 def price(self):
 	del self._price

@staticmethod

staticmethod修饰过的方法叫静态方法,可以直接通过类调用方法,这样做的好处是执行效率比较高,也可以通过实例调用该方法。

from datetime import datetime
TIME_FORMAT_STR = "%Y/%m/%d/ %H:%M:%S"
class TimeUtil():
 @staticmethod
 def timestamp_to_utc_str(timestamp: float, format_str=TIME_FORMAT_STR) -> str:
	 """时间戳转utc-0时区的时间"""
	 datetime_obj: datetime = datetime.utcfromtimestamp(timestamp)
	 return datetime_obj.strftime(format_str)
	@staticmethod
	def timestamp_to_local_str(timestamp: float, format_str=TIME_FORMAT_STR) -> str:
	 """时间戳转当前本地时区的时间"""
	 datetime_obj: datetime = datetime.fromtimestamp(timestamp)
	 return datetime_obj.strftime(format_str)

@classmethod

classmethod修饰过的方法叫类方法,可以直接通过类或者实例调用方法

利用@classmethod实现单例模式

from datetime import datetime
class SingletonBase(object):
 __instance = None
 @classmethod
 def get_instance(cls, *arg, **kwargs):
 if cls.__instance is None:
 cls.__instance = cls()
 cls.__instance.init(*arg, **kwargs)
 return cls.__instance
 def init(self, *arg, **kwargs):
 pass
class TestMgr(SingletonBase):
 def init(self, *arg, **kwargs):
 print(f'I am TestMgr Singleton, arg:{arg}, kwargs:{kwargs}')
if __name__ == '__main__':
	test_mgr = TestMgr.get_instance('hello', 'world', time=datetime.now().strftime("%Y/%m/%d/ %H:%M:%S"))

自定义装饰器

python装饰器2:类装饰器
python装饰器的4种类型:函数装饰函数、函数装饰类、类装饰函数、类装饰类

函数的装饰器

无参数

import time
all_func = {}
# 用于函数注册的装饰器
def register_func(func):
 all_func[func.__name__] = func
 return func
# 用于统计函数耗时的装饰器
def calc_time(func):
 def inner_func():
 start = time.time()
 func()
 end = time.time()
 print(
 f'call {func.__name__} func, cost time:{int(end - start)} second')
 return inner_func
@calc_time
def test_func4():
 time.sleep(2)
if __name__ == '__main__':
 print('=========================')
 test_func4()
# 结果
=========================
call test_func4 func, cost time:2 second

有参数

# -*- coding:utf-8 -*-
from functools import wraps
test_dict = {}
def Decorator(arg1=0, arg2=1):
 def inner_func(func):
 funcName = func.__name__
 test_dict[funcName] = func
 # 没有return func,则该装饰器默认返回None,会导致被Decorator装饰后的函数无法被调用,
 # 有return func,则被Decorator装饰后的函数还是被赋值为原函数,后续还可以调用
 # return func
 return inner_func
def decorator(*arg):
 return Decorator(*arg)
def Decorator2(arg1=0, arg2=1):
 def inner_func(func):
 @wraps(func)
 def wrapper(*args, **kwargs):
 # do something
 return func(*args, **kwargs)
 funcName = func.__name__
 test_dict[funcName] = func
 # 有return wrapper,则被Decorator2装饰后的函数还是被赋值为原函数,后续还可以调用
 return wrapper
 return inner_func
def decorator2(*arg):
 return Decorator2(*arg)
@Decorator()
def test_func_a():
 pass
@decorator()
def test_func_b():
 pass
@Decorator2()
def test_func_c():
 pass
@decorator2()
def test_func_d():
 pass
if __name__ == "__main__":
 print(test_dict)
# 结果
{'test_func_a': , 'test_func_b': , 'test_func_c': , 'test_func_d': }

类的装饰器

无参数

# decorator1(cls)返回一个函数inner_func的写法
def decorator1(cls):
 def inner_func(a):
 print('class name:', cls.__name__)
 return cls(a)
 return inner_func
# decorator2(cls)返回一个类inner_cls的写法
def decorator2(cls):
 class inner_cls:
 def __init__(self, *args, **kwargs):
 self.wrapped_obj = cls(*args, **kwargs)
 def __getattr__(self, name):
 return getattr(self.wrapped_obj, name)
 return inner_cls
@decorator1
class FooClass1():
 def __init__(self, a):
 self.a = a
 def fun(self):
 print('self.a =', self.a)
@decorator2
class FooClass2():
 def __init__(self, a):
 self.a = a
 def fun(self):
 print('self.a =', self.a)
foo1 = FooClass1('Foo1_a')
foo1.fun()
foo2 = FooClass2('Foo2_a')
foo2.fun()

有参数

1.通过装饰器实现子类的自动注册

all_subcls = {}
def register_subcls(base_cls_name):
 def decorate(sub_cls):
 if base_cls_name not in all_subcls :
 all_subcls[base_cls_name] = {}
 all_subcls[base_cls_name][sub_cls.__name__] = sub_cls
 return decorate
def get_subcls(base_cls_name, sub_cls_name):
	if base_cls_name not in all_subcls:
	return None
	else:
	return all_subcls[base_cls_name].get(sub_cls_name)
# 使用
class TestBase1(object):
	@staticmethod
	def create_instance(sub_cls_name, *args, **kwargs):
	sub_cls = get_subcls('TestBase1', sub_cls_name)
	return sub_cls(*args, **kwargs) if sub_cls else None
	
	def __init__(self, *args, **kwargs):
	self.args = args
	self.kwargs = kwargs
	
	def print_self(self):
	print(f'TestBase1, args:{args}, kwargs:{kwargs}')
@register_subcls('TestBase1')
class TestA(TestBase1):
	def print_self(self):
	print(f'TestA, args:{args}, kwargs:{kwargs}')
@register_subcls('TestBase1')
class TestB(TestBase1):
	def print_self(self):
	print(f'TestB, args:{args}, kwargs:{kwargs}')
  1. 通过装饰器给类增加静态函数/类函数/成员函数
# 实例方法
def add_instance_method(cls):
 def inner_func(func):
 setattr(cls, func.__name__, func)
 return inner_func
# 类方法
def add_class_method(cls):
 def inner_func(func):
 setattr(cls, func.__name__, classmethod(func))
 return inner_func
# 静态方法
def add_static_method(cls):
 def inner_func(func):
 setattr(cls, func.__name__, staticmethod(func))
 return inner_func
def add_func_decorator(cls):
 @add_instance_method(cls)
 def test_instance_func(self):
 print('test_instance_func')
 @add_class_method(cls)
 def test_class_func(cls):
 print('test_class_func')
 @add_static_method(cls)
 def test_static_func():
 print('test_static_func')
 return cls
@add_func_decorator
class TestClass():
 def __init__(self):
 pass
if __name__ == '__main__':
 TestClass.test_class_func()
 TestClass.test_static_func()
 test_obj = TestClass()
 test_obj.test_instance_func()
# 结果
test_class_func
test_static_func
test_instance_func

@functools.wraps

functools.wraps对我们的装饰器函数进行了装饰之后,消除了装饰器对原函数的影响。
具体Python装饰器深度解析

装饰器类

之前都是用函数来构造装饰器,其实类也可以用来构建装饰器
装饰器类

先熟悉一下Python __call__()方法
Python 中,凡是可以将 () 直接应用到自身并执行,都称为可调用对象。可调用对象包括自定义的函数、Python 内置函数以及本节所讲的类实例对象。
__ call__()方法的功能类似于在类中重载 () 运算符,使得类实例对象可以像调用普通函数那样,以“对象名()”的形式使用。

from functools import wraps
class logit(object):
 def __init__(self, logfile='out.log'):
 self.logfile = logfile
 def __call__(self, func):
 @wraps(func)
 def wrapped_function(*args, **kwargs):
 log_string = func.__name__ + " was called"
 print(log_string)
 with open(self.logfile, 'a') as fp:
 fp.write(log_string + '\n')
 # 发送一个通知
 self.notify()
 return func(*args, **kwargs)
 return wrapped_function
 def notify(self):
 pass
 
# 使用
@logit()
def myfunc1():
 pass

装饰器实现单例模式

Python单例模式(Singleton)的N种实现

1.使用函数装饰器实现单例

def Singleton(cls):
 _instance = {}
 def inner():
 if cls not in _instance:
 _instance[cls] = cls()
 return _instance[cls]
 return inner
 
@Singleton
class Cls(object):
 def __init__(self):
 pass
cls1 = Cls()
cls2 = Cls()
print(id(cls1) == id(cls2))

2.使用类装饰器实现单例

class Singleton(object):
 def __init__(self, cls):
 self._cls = cls
 self._instance = {}
 def __call__(self):
 if self._cls not in self._instance:
 self._instance[self._cls] = self._cls()
 return self._instance[self._cls]
@Singleton
class Cls2(object):
 def __init__(self):
 pass
cls1 = Cls2()
cls2 = Cls2()
print(id(cls1) == id(cls2))
# 同时,由于是面对对象的,这里还可以这么用
class Cls3():
 pass
Cls3 = Singleton(Cls3)
cls3 = Cls3()
cls4 = Cls3()
print(id(cls3) == id(cls4))
作者:selfsongs原文地址:https://blog.csdn.net/qq_35987777/article/details/121623737

%s 个评论

要回复文章请先登录注册