专业编程基础技术教程

网站首页 > 基础教程 正文

8 Python对象-内部类型之代码对象&函数对象

ccvgpt 2024-10-10 05:05:30 基础教程 37 ℃

内部类型,一般程序员不会直接使用,为了知识的完整性还是要介绍一下。

代码对象

代码对象:编译过的Python源代码片段,是一个可执行对象。可以通过compile()内建函数得到代码对象。代码对象可以被exec命令或eval()内建函数来执行。

8 Python对象-内部类型之代码对象&函数对象

# 定义源代码字符串
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__,会打印如下的代码。

Tags:

最近发表
标签列表