首页 > 编程语言 >Python-codeobject

Python-codeobject

时间:2022-12-31 09:44:39浏览次数:34  
标签:__ code co Python .__ codeobject print stacksize

普通函数的code本地存储情况

def f():
    pass
print(f.__code__)
#<code object f at 0x0000018A7AC6F1B0, file "E:\zwx901323\test_position\easy.py", line 1>
print(dir(f.__code__))
"""
['__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', 
'__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'co_argcount', 'co_cellvars', 'co_code', 'co_consts',
'co_filename', 'co_firstlineno', 'co_flags', 'co_freevars', 'co_kwonlyargcount', 'co_lines', 'co_linetable', 'co_lnotab', 'co_name', 'co_names', 'co_nlocals', 'co_posonlyargcount',
'co_stacksize', 'co_varnames', 'replace'] """ print(f.__code__.co_code) #b'd\x00S\x00'

一些常见用法

code = f.__code__
print(code.co_name)  #定义的code名字-函数名称
print(code.co_filename)#函数定义所在哪个文件
print(code.co_Inotab)  #代码的行数,以二进制保存起来
"""
f
E:\zwx901323\test_position\easy.py
b'\x00\x01'
"""

codeframe需要的数据

print(code.co_flags)
print(code.co_stacksize)
"""
67
1
""""

 

 

函数参数相关

print(code.co_argcount)
print(code.co_posonlyargcount)
print(code.co_kwonlyargcount)

 

标签:__,code,co,Python,.__,codeobject,print,stacksize
From: https://www.cnblogs.com/zwx901323/p/17016229.html

相关文章