Python的编译结果-code对象与pyc文件(Python 源码剖析第七章)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
1 >>> source = open('e:\python\p9.py').read()
2 >>> source
3 'for a in range(1, 1000):\n\tfor b in range(1, 1000):\n\t\tfor c in range(334, 999):\n\t\t\tif(a < b and b < c and (a+b+c==1000) and (a*a+b*b==c*c)):\n\t\t\t\tprint a* b * c\n'
4 >>> code = compile(source, 'p.py', 'exec')
5 >>> type(code)
6 <type 'code'>
7 >>> dir(code)
8 ['__class__', '__cmp__', '__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_lnotab', 'co_name', 'co_names', 'co_nlocals', 'co_stacksize', 'co_varnames']
9 >>> print code.co_names
10 ('range', 'a', 'b', 'c')
11 >>> print code.co_filename
12 p.py
13 >>>
2 >>> source
3 'for a in range(1, 1000):\n\tfor b in range(1, 1000):\n\t\tfor c in range(334, 999):\n\t\t\tif(a < b and b < c and (a+b+c==1000) and (a*a+b*b==c*c)):\n\t\t\t\tprint a* b * c\n'
4 >>> code = compile(source, 'p.py', 'exec')
5 >>> type(code)
6 <type 'code'>
7 >>> dir(code)
8 ['__class__', '__cmp__', '__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_lnotab', 'co_name', 'co_names', 'co_nlocals', 'co_stacksize', 'co_varnames']
9 >>> print code.co_names
10 ('range', 'a', 'b', 'c')
11 >>> print code.co_filename
12 p.py
13 >>>