在该文(基础篇)中简单介绍了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, *

最低0.47元/天 解锁文章
684

被折叠的 条评论
为什么被折叠?



