Python 自定义函数的特殊属性(收藏专用)
admin
2023-07-31 00:45:31
0

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(强制关键字参数)”


相关内容

热门资讯

Mobi、epub格式电子书如... 在wps里全局设置里有一个文件关联,打开,勾选电子书文件选项就可以了。
定时清理删除C:\Progra... C:\Program Files (x86)下面很多scoped_dir开头的文件夹 写个批处理 定...
scoped_dir32_70... 一台虚拟机C盘总是莫名奇妙的空间用完,导致很多软件没法再运行。经过仔细检查发现是C:\Program...
小程序支付时提示:appid和... [Q]小程序支付时提示:appid和mch_id不匹配 [A]小程序和微信支付没有进行关联,访问“小...
500 行 Python 代码... 语法分析器描述了一个句子的语法结构,用来帮助其他的应用进行推理。自然语言引入了很多意外的歧义,以我们...
pycparser 是一个用... `pycparser` 是一个用 Python 编写的 C 语言解析器。它可以用来解析 C 代码并构...
微信小程序使用slider实现... 众所周知哈,微信小程序里面的音频播放是没有进度条的,但最近有个项目呢,客户要求音频要有进度条控制,所...
65536是2的几次方 计算2... 65536是2的16次方:65536=2⁶ 65536是256的2次方:65536=256 6553...
Apache Doris 2.... 亲爱的社区小伙伴们,我们很高兴地向大家宣布,Apache Doris 2.0.0 版本已于...
项目管理和工程管理的区别 项目管理 项目管理,顾名思义就是专注于开发和完成项目的管理,以实现目标并满足成功标准和项目要求。 工...