7. 函数的属性
函数的特殊属性
属性
说明
__name__
函数的名称(字符串形式)。
__doc__
函数的文档字符串(docstring)
__module__
函数定义所在的模块名(如果是当前模块,返回
"__main__ "
)__defaults__
函数的默认参数值(元组形式)。
__annotations__
函数的类型标注
__closure__
闭包引用的外部变量(如果是闭包)。
示例
# 此示例示意函数的特殊属性
def my_function(a: int=666, b: int=999) -> int:
'''此函数用于累加两个整数,返回加法的结果!'''
pass
print(my_function.__name__) # 函数名
print(my_function.__doc__) # 文档字符串
print(my_function.__module__) # 模块名(当前为主模块)
print(my_function.__defaults__) # 缺省参数的值(元组)
print(my_function.__annotations__) # 函数的类型标注
print(my_function.__closure__) # 闭包引用的外部变量