在该文(基础篇)中简单介绍了python自定义注解与获取,但该文中的注解定义方式存在缺陷:同一方法上无法区分哪个信息是由哪个注解注入的(多注解情况)
简单理解下就是:
@annotationA(A_info="this is A",value="功能A")
@annotationB(B_info="this is B",value="功能B")
def test_fun():
print("test_fun...")
if __name__ == "__main__":
annos = test_annotation.__annotations__
print(annos)
打印:
('A_info', 'this is A')
('value', '功能A')
('B_info', 'this is B')
('value', '功能B')
@annotationA与@annotationB两个注解将自身信息注入到test_fun方法中,出现了两个value,因此无法区分它们分别属于哪个注解
如何区分?
# 基类注解
def base(annotation):
def decorator_annos(*args, **kwargs):
# print(args,kwargs)
def decorator(fn):
sub_annos_name = annotation.__name__
if fn.__annotations__.get(sub_annos_name) is None:
# 以子类注解名作为key生成一个字典,用于区分注解
fn.__annotations__[sub_annos_name] = {}
# 放钩子,调用子类注解
annotation(fn, *args, **kwargs)
return fn
return decorator
return decorator_annos
@base
def annotationA(fn,*args,**kwds):
for k, v in kwds.items():
fn.__annotations__["annotationA"][k] = v
@base
def annotationB(fn,*args,**kwds):
for k, v in kwds.items():
fn.__annotations__["annotationB"][k] = v
@annotationA(A_info="this is A",value="功能A")
@annotationB(B_info="this is B",value="功能B")
def test_fun():
pass
print(test_fun.__annotations__)
打印test_fun方法的注解信息:
{'annotationB': {'B_info': 'this is B', 'value': '功能B'}, 'annotationA': {'A_info': 'this is A', 'value': '功能A'}}
这样,在多注解情况下就可以区分哪些信息是由哪些注解注入的了
下面写一个更符合实际运用的注解,该注解实现了类似spring注解value字段可以省略的功能,即@annotation_A("功能A")等价于@annotation_A(value="功能A")
@base
def annotationA(fn,*args,**kwds):
if args != None and len(args) != 0:
if len(args) > 1:
raise RuntimeError("nums of @annotationA parameter can only be one! eg. @annotationA('功能A')")
fn.__annotations__["annotationA"]["value"] = args[0]
elif len(kwds) != 1 or (not kwds.keys().__contains__("value")):
raise RuntimeError("If the form of @annotationA parameter is k,v \
, then key can only be 'value' str! eg. @annotationA(value='功能A')")
else:
for k, v in kwds.items():
fn.__annotations__["annotationA"][k] = v
@annotationA("this is A function")
def test_fun1():
pass
@annotationA(value="this is A function")
def test_fun2():
pass
assert test_fun1.__annotations__ == test_fun2.__annotations__
print(test_fun1.__annotations__)
print(test_fun2.__annotations__)
打印:
{'annotationA': {'value': 'this is A function'}}
{'annotationA': {'value': 'this is A function'}}
这样,注解的value字段就可以省略了