Python 中通过函数定义所创建的用户自定义函数对象均具有一些特殊属性,需要注意的是这里介绍的是自定义函数(function类型)的特殊属性,而非方法(method 类型)的特殊属性,函数和方法的特熟属性以及默认的返回值可能不尽相同。

对于大多数特殊属性,可以通过下面这个例子示范一下:

12345678910111213 class Test():     def func(self, v = \’dog\’):        \’\’\’这里演示一个闭包函数\’\’\’         name = \’dobi\’        def inn_func(age = 1):            print(name, v, age)         return inn_func test = Test()clsfunc = test.func()

首先看一下方法与函数的区别:实例的函数为bound method,而类的函数以及闭包均为function,需要强调的是 Python 2.x 中类的函数为unbound method,这点与Python 3.x 有所不同,本文则基于 Python 3.51 整理。

123456 print(Test.func)# print(test.func)# >print(clsfunc)# .inn_func at 0x0000020F071D7F28>

__doc__

可写;用于获取函数的文档说明,如果没有,则返回 None

12345 print(\’Test.func.__doc__:\’, Test.func.__doc__)# Test.func.__doc__: 这里演示一个闭包函数Test.func.__doc__ = \’ddd\’  #注意,这里是 Test,不能是 testprint(\’Test.func.__doc__:\’, Test.func.__doc__)# Test.func.__doc__: ddd

__name__

可写;获取函数的名称。

12345 print(\’Test.func.__name__:\’, Test.func.__name__)# Test.func.__name__: funcTest.func.__name__ = \’pet\’print(\’Test.func.__name__:\’, Test.func.__name__)# Test.func.__name__: pet

__qualname__

可写;获取函数的qualname:点示法显示函数名称、所在的类、模块等梯级地址。

12345 print(\’Test.func.__qualname__:\’, Test.func.__qualname__)# Test.func.__qualname__: Test.funcTest.func.__qualname__ = \’path\’print(\’Test.func.__qualname__:\’, Test.func.__qualname__)# Test.func.__qualname__: path

__module__

可写;返回函数所在的模块,如果无则返回None

12345 print(\’Test.func.__module__:\’, Test.func.__module__)# Test.func.__module__: __main__Test.func.__module__ = \’a\’print(\’Test.func.__module__:\’, Test.func.__module__)# Test.func.__module__: a

__defaults__

可写;以元组的形式返回函数的默认参数,如果无默认参数则返回None

12345678910 print(\’Test.func.__defaults__:\’, Test.func.__defaults__)# Test.func.__defaults__: (\’dog\’,)Test.func.__defaults__ = (\’cat\’,)print(\’Test.func.__defaults__:\’, Test.func.__defaults__)# Test.func.__defaults__: (\’cat\’,)print(\’clsfunc.__defaults__:\’, clsfunc.__defaults__)# clsfunc.__defaults__: (1,)clsfunc.__defaults__ = (2,)print(\’clsfunc.__defaults__:\’, clsfunc.__defaults__)# clsfunc.__defaults__: (2,)

__code__

可写;返回已编译的函数对象。

123456789 print(\’Test.func.__code__:\’, Test.func.__code__)# Test.func.__code__:  def func2():print(\’cat\’)Test.func.__code__ = func2.__code__Test.func()# catprint(\’Test.func.__code__:\’, Test.func.__code__)# Test.func.__code__:

__globals__

只读,以字典的形式返回函数所在的全局命名空间所定义的全局变量。

123456789101112131415 print(\’Test.func.__globals__:\’, Test.func.__globals__)# Test.func.__globals__: {# \’__cached__\’: None, # \’Test\’: , # \’__builtins__\’: , # \’func2\’: , # \’__spec__\’: None, # \’__doc__\’: None, # \’__file__\’: \’D:\\\\…\\\\a.py\’, # \’test\’: <__main__.Test object at 0x0000020F077E5DA0>, # \’clsfunc\’: .inn_func at 0x0000020F071D7F28>, # \’__package__\’: None, # \’__name__\’: \’__main__\’, # \’__loader__\’: <_frozen_importlib_external.SourceFileLoader object at 0x0000020F07289828># }

__dict__

可写;以字典的形式返回命名空间所支持的任意自定义的函数属性。

12 print(\’Test.func.__dict__:\’, Test.func.__dict__)# Test.func.__dict__: {}

__closure__

只读;以包含cell的元组形式返回闭包所包含的自由变量。

123456789 print(\’Test.func.__closure__:\’, Test.func.__closure__)# Noneprint(\’clsfunc.__closure__:\’, clsfunc.__closure__)# clsfunc.__closure__: (# , # # )print(\’clsfunc.__closure__[x]:\’, clsfunc.__closure__[0].cell_contents, clsfunc.__closure__[1].cell_contents)# clsfunc.__closure__[x]: dobi dog

__annotations__

可写;具体详见“Python 的函数注释”

__kwdefaults__

可写,具体详见 “Python 的 Keyword-Only Arguments(强制关键字参数)”