python函数对象
from typing import Optional
class Item():
name: str
description: Optional[str] = None
price: float
tax: Optional[float] = None
def fun(name,age:Item,sex:str="1"):
print(name,age,sex)
print(fun.__annotations__)
print(fun.__code__.co_varnames)
for key in fun.__code__.co_varnames:
if fun.__annotations__.__contains__(key):
print(key,fun.__annotations__.get(key))
输出
{'age': <class '__main__.Item'>, 'sex': <class 'str'>}
('name', 'age', 'sex')
age <class '__main__.Item'>
sex <class 'str'>
类型检查
from typing import Union
a = Union[str,None]
print(a.__args__)
print(type(None) in a.__args__)
print(str in a.__args__)
print(list in a.__args__)
输出
(<class 'str'>, <class 'NoneType'>)
True
True
False