Python源码学习(六)-PyCodeObject初探

demo1.py

def f():
	c=1
	print("Hello")

a=9
b=5	
f();

co.py

source = open('demo1.py').read()
co = compile(source, 'demo1.py', 'exec')
print("type:")
print(type(co))
print("dir(co)")
print(dir(co))
print("co.co_name:" + co.co_name)
print("co.co_names:")
for i in co.co_names:
	print(i)
print("-" * 30)

print("co.co_code:")
print(co.co_code)
print("-" * 30)

print("co.co_consts:")
for i in co.co_consts:
	print(i)
print("-" * 30)

print("co.co_filename:")
print(co.co_filename)
print("-" * 30)

print("co.nlocals:")
print(co.co_nlocals)
print("-" * 30)

print("co.co_stacksize:")
print(co.co_stacksize)
print("co.co_lnotab;")
print(co.co_lnotab)

print("-" * 30)
print("co.co_varnames:")
for i in co.co_varnames:
	print(i)

结果

type:
<class 'code'>
dir(co):
['__class__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__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_lnotab', 'co_name', 'co_names', 'co_nlocals', 'co_stacksize', 'co_varnames']
co.co_name:<module>
co.co_names:
f
a
b
------------------------------
co.co_code:
b'd\x00\x00\x84\x00\x00Z\x00\x00d\x01\x00Z\x01\x00d\x02\x00Z\x02\x00e\x00\x00\x83\x00\x00\x01d\x03\x00S'
------------------------------
co.co_consts:
<code object f at 0x00C0D2F0, file "demo1.py", line 1>
9
5
None
------------------------------
co.co_filename:
demo1.py
------------------------------
co.nlocals:
0
------------------------------
co.co_stacksize:
1
co.co_lnotab;
b'\t\x04\x06\x01\x06\x01'
------------------------------
co.co_varnames:


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值