Open anancds opened 4 years ago
co_name给出函数名称。 co_argcount是位置参数的数量,包括具有默认值的参数。 co_nlocals是函数使用的局部变量数,包括参数。 co_varnames是一个包含局部变量名称的元组,以参数名称开头。 co_cellvars是一个元组,包含嵌套函数引用的局部变量的名称。 co_freevars是一个包含自由变量名称的的元组。 co_code是表示字节码指令序列的字符串。 co_consts是一个包含字节码使用的文字的元组。如果代码对象表示函数,则co_consts的第一项是函数的文档字符串,如果文档字符串未定义,则是None。 co_names是一个包含字节码使用的名称的元组。 co_filename是编译代码的文件名。 co_firstlineno是函数的第一个行号。 co_lnotab是一个字符串,用于编码从字节码偏移到行号的映射(更详细的信息参考解释的源代码)。 co_stacksize是所需的堆栈大小(包括局部变量)。 co_flags是一个整数,作为编码解释器的标志使用。以下标志位定义为co_flags:0x04如果函数使用arguments语法接受任意数量的位置参数,则设置位; 0x08如果函数使用keywords语法接受任意关键字参数,则设置 bit 。
def hello(): """hello function""" print('hello world', '你好') dis.dis(hello) """dis.dis(hello)输出结果 8 0 LOAD_GLOBAL 0 (print) 2 LOAD_CONST 1 ('hello world') 4 LOAD_CONST 2 ('你好') 6 CALL_FUNCTION 2 8 POP_TOP 10 LOAD_CONST 3 (None) 12 RETURN_VALUE """ print(hello.code.co_names) # ('print',) print(hello.code.co_consts) # ('hello function', 'hello world', '你好', None) print(hello.code.co_argcount) # 0
thank you all for showing up and hopefully you learn something
def hello(): print('hello world')
print(hello.code) # <code object hello at 0x035B4F98, file "M:/demo/MyAI/AI/part2/demo.py", line 20> print(hello.code.co_name) # hello print(hello.code.co_argcount) # 0 print(hello.code.co_nlocals) # 0 print(hello.code.co_varnames) # () print(hello.code.co_cellvars) # () print(hello.code.co_freevars) # () print(hello.code.co_code) # b't\x00d\x01\x83\x01\x01\x00d\x00S\x00' print(hello.code.co_consts) # (None, 'hello world') print(hello.code.co_names) # ('print',) print(hello.code.co_filename) # M:/demo/MyAI/AI/part2/demo.py print(hello.code.co_firstlineno) # 20 print(hello.code.co_lnotab) # b'\x00\x01' print(hello.code.co_stacksize) # 2 print(hello.code.co_flags) # 67