zoukankan      html  css  js  c++  java
  • 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'dx00x00x84x00x00Zx00x00dx01x00Zx01x00dx02x00Zx02x00ex00x00x83x00x00x01dx03x00S'
    ------------------------------
    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'	x04x06x01x06x01'
    ------------------------------
    co.co_varnames:
    


  • 相关阅读:
    NetBeans 时事通讯(刊号 # 147 May 18, 2011)
    NetBeans 时事通讯(刊号 # 146 May 13, 2011)
    准备入职支付宝
    NetBeans 时事通讯(刊号 # 145 May 05, 2011)
    GAE 博客——B3log Solo 0.2.6 正式版发布了!
    用C语言实现一个简单的HTTP客户端(HTTP Client)
    Grack Ruby/Rack Git SmartHTTP Server Handler
    Cpy
    pyhttp 用Python演示Web服务器/客户端对HTTP协议的解析
    用C语法来写Python代码
  • 原文地址:https://www.cnblogs.com/phisy/p/3363590.html
Copyright © 2011-2022 走看看