Python 自定义函数的特殊属性(收藏专用)

来源:互联网 发布:python interperter 编辑:程序博客网 时间:2024/05/16 01:03

来源:xu_zhoufeng 

链接:https://segmentfault.com/a/1190000005685090

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

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

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

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

print(Test.func)# <function Test.func at 0x0000020F0766E268>print(test.func)# <bound method Test.func of <__main__.Test object at 0x0000020F077E5DA0>>print(clsfunc)# <function Test.func.<locals>.inn_func at 0x0000020F071D7F28>

__doc__

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

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__

可写;获取函数的名称。

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:点示法显示函数名称、所在的类、模块等梯级地址。

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

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

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__

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

print('Test.func.__code__:', Test.func.__code__)# Test.func.__code__: <code object func at 0x0000020F072C2F60, file "D:\...\a.py", line 4>def func2():print('cat')Test.func.__code__ = func2.__code__Test.func()# catprint('Test.func.__code__:', Test.func.__code__)# Test.func.__code__: <code object func2 at 0x0000020F072C5780, file "D:\...\a.py", line 38>

__globals__

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

print('Test.func.__globals__:', Test.func.__globals__)# Test.func.__globals__: {# '__cached__': None, # 'Test': <class '__main__.Test'>, # '__builtins__': <module 'builtins' (built-in)>, # 'func2': <function func2 at 0x0000020F077D3C80>, # '__spec__': None, # '__doc__': None, # '__file__': 'D:\\...\\a.py', # 'test': <__main__.Test object at 0x0000020F077E5DA0>, # 'clsfunc': <function Test.func.<locals>.inn_func at 0x0000020F071D7F28>, # '__package__': None, # '__name__': '__main__', # '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x0000020F07289828># }

__dict__

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

print('Test.func.__dict__:', Test.func.__dict__)# Test.func.__dict__: {}

__closure__

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

print('Test.func.__closure__:', Test.func.__closure__)# Noneprint('clsfunc.__closure__:', clsfunc.__closure__)# clsfunc.__closure__: (# <cell at 0x0000020F071FE708: str object at 0x0000020F07289998>, # <cell at 0x0000020F072B8C78: str object at 0x0000020F0766C538># )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(强制关键字参数)”

0 0