网站首页 > 基础教程 正文
内部类型,一般程序员不会直接使用,为了知识的完整性还是要介绍一下。
代码对象
代码对象:编译过的Python源代码片段,是一个可执行对象。可以通过compile()内建函数得到代码对象。代码对象可以被exec命令或eval()内建函数来执行。
# 定义源代码字符串
source_code = """
def say_hello(name):
print(f"Hello, {name}!")
say_hello("World")
"""
# 使用compile函数编译源代码字符串为代码对象
# mode参数'exec'表示这是一个模块、语句或序列的语句
code_object = compile(source_code, 'example', 'exec')
# 使用exec函数执行代码对象
exec(code_object)
# 输出: Hello, World!
# 如果我们想要定义一个可以在后续代码中使用的函数,我们可以这样做
# 注意:这里我们直接执行代码对象,而不是将函数存储在某个变量中
# 但为了演示,我们假设有一个场景需要将函数存储在变量中
# 注意:下面的代码实际上不会工作,因为它只是定义了函数而没有调用它
# 但这展示了如何将函数定义包含在代码对象中
# 定义只包含函数定义的源代码
function_source = """
def greet(name):
print(f"Greetings, {name}!")
"""
# 编译为代码对象
function_code_object = compile(function_source, 'greet_func', 'exec')
# 执行代码对象以定义函数
exec(function_code_object)
# 在此处可以调用greet函数,如果在IDE中运行此处会报Unresolved reference 'greet'
greet("Python")
# 输出: Greetings, Python!
代码对象本身不包含任何执行环境信息,它只是用户自定义函数的核心,在被执行时才动态获得上下文。
函数对象
实际上代码对象只是函数对象的一个属性。函数除了有代码对象属性外,还有其他必须得属性,如:函数名、文档字符串、默认参数、全局命名空间等。
def example_function(arg1, arg2=42, *, kwarg=None):
"""This is an example function."""
print(f"arg1: {arg1}, arg2: {arg2}, kwarg: {kwarg}")
# 访问函数对象的属性
print(example_function.__name__) # 输出: example_function
print(example_function.__doc__) # 输出: This is an example function.
print(example_function.__module__) # 输出: __main__(如果这段代码是直接在Python脚本中运行的)
print(example_function.__defaults__) # 输出: (42,)
print(example_function.__code__) # 输出: 一个code对象,具体内容可能因Python版本和解释器而异
# 尝试访问__dict__(对于普通函数,这通常是空的)
print(example_function.__dict__) # 输出: {}(或者可能抛出AttributeError,取决于Python版本和解释器)
# 访问注解
print(example_function.__annotations__) # 输出: {'kwarg': None}
函数的默认参数可以通过__defaults__属性访问。
def example_function(arg1, arg2=42, arg3='hello'):
print(f"arg1: {arg1}, arg2: {arg2}, arg3: {arg3}")
# 访问函数的默认参数值
print(example_function.__defaults__) # 输出: (42, 'hello')
# 注意,只有位置参数(即非关键字参数)的默认值会被包含在__defaults__中。
# 如果函数有*args或**kwargs参数,它们不会影响__defaults__的内容。
# 关键字参数的默认值是通过函数的注解(__annotations__)来记录的,但它们并不直接作为默认值存储。
如果要访问函数对象的全部参数,需要利用inspect模块的signature()函数获取。
import inspect
def example_function(arg1, arg2=42, *args, kwarg=None, **kwargs):
pass
# 获取函数的签名
sig = inspect.signature(example_function)
# 打印整个签名
print(sig)
# 遍历参数
for param in sig.parameters.values():
print(f"{param.name}: {param.kind} {param.default if param.default is not inspect.Parameter.empty else 'no default'}")
输出结果:
还可以使用函数对象的__code__属性的co_varnames属性获取参数名。
def example_function(x, y):
return x + y
# 访问函数对象的__code__属性
code_object = example_function.__code__
# 现在,你可以通过code_object来访问函数的编译后信息
# 例如,查看函数的参数名
print(code_object.co_varnames)
输出结果:
关于函数对象的代码对象(__code__)要利用dis.dis()函数打印字节码。__code__不能直接打印。
def example_function(x, y):
return x + y
# 访问函数对象的__code__属性
code_object = example_function.__code__
# 查看函数的字节码
import dis
dis.dis(example_function) # 这将直接打印函数的字节码,而不是通过__code__属性访问
# 如果你确实需要通过__code__属性来操作字节码,你可能需要使用一些更底层的库,
# 如`types`模块中的`CodeType`,但这通常不是必要的。
print(code_object)
直接打印__code__,会打印如下的代码。
猜你喜欢
- 2024-10-10 Linux系统常用命令大全 linux系统常用命令大全详解
- 2024-10-10 Redis数据类型与常用指令示例 redis数据类型和用法
- 2024-10-10 深入理解Redis原理与应用,总结10个必定掌握要点,5分钟彻底看懂
- 2024-10-10 sqlserver数据库中的高级知识点有哪些?
- 2024-10-10 程序员的福音 - Apache Commons Exec
- 2024-10-10 Linux常用命令分类详解|文件搜索 linux命令分类总结
- 2024-10-10 linux性能工具perf工作原理简析 linux perl
- 2024-10-10 Python-调用系统命令或脚本,快速完成任务
- 2024-10-10 「Docker」命令使用大全,全集一览
- 2024-10-10 DOS常用命令集合 dos所有命令的命令格式
- 最近发表
- 标签列表
-
- gitpush (61)
- pythonif (68)
- location.href (57)
- tail-f (57)
- pythonifelse (59)
- deletesql (62)
- c++模板 (62)
- css3动画 (57)
- c#event (59)
- linuxgzip (68)
- 字符串连接 (73)
- nginx配置文件详解 (61)
- html标签 (69)
- c++初始化列表 (64)
- exec命令 (59)
- canvasfilltext (58)
- mysqlinnodbmyisam区别 (63)
- arraylistadd (66)
- node教程 (59)
- console.table (62)
- c++time_t (58)
- phpcookie (58)
- mysqldatesub函数 (63)
- window10java环境变量设置 (66)
- c++虚函数和纯虚函数的区别 (66)