55----------
66问题
77----------
8- You want to know in detail what your code is doing under the covers by disassembling
9- it into lower-level byte code used by the interpreter.
8+ 你想通过将你的代码反编译成低级的字节码来查看它底层的工作机制。
109
1110|
1211
1312----------
1413解决方案
1514----------
16- The dis module can be used to output a disassembly of any Python function. For
17- example:
15+ ``dis `` 模块可以被用来输出任何Python函数的反编译结果。例如:
1816
1917.. code-block :: python
2018
@@ -34,9 +32,8 @@ example:
3432----------
3533讨论
3634----------
37- The dis module can be useful if you ever need to study what’s happening in your program
38- at a very low level (e.g., if you’re trying to understand performance characteristics).
39- The raw byte code interpreted by the dis() function is available on functions as follows:
35+ 当你想要知道你的程序底层的运行机制的时候,``dis `` 模块是很有用的。比如如果你想试着理解性能特征。
36+ 被 ``dis() `` 函数解析的原始字节码如下所示:
4037
4138.. code-block :: python
4239
@@ -46,8 +43,7 @@ The raw byte code interpreted by the dis() function is available on functions as
4643 \x01\x00\x01d\x00\x00S"
4744 >> >
4845
49- If you ever want to interpret this code yourself, you would need to use some of the
50- constants defined in the opcode module. For example:
46+ 如果你想自己解释这段代码,你需要使用一些在 ``opcode `` 模块中定义的常量。例如:
5147
5248.. code-block :: python
5349
@@ -60,9 +56,8 @@ constants defined in the opcode module. For example:
6056 ' LOAD_FAST'
6157 >> >
6258
63- Ironically, there is no function in the dis module that makes it easy for you to process
64- the byte code in a programmatic way. However, this generator function will take the raw
65- byte code sequence and turn it into opcodes and arguments.
59+ 奇怪的是,在 ``dis `` 模块中并没有函数让你以编程方式很容易的来处理字节码。
60+ 不过,下面的生成器函数可以将原始字节码序列转换成 ``opcodes `` 和参数。
6661
6762.. code-block :: python
6863
@@ -86,15 +81,15 @@ byte code sequence and turn it into opcodes and arguments.
8681 oparg = None
8782 yield (op, oparg)
8883
89- To use this function, you would use code like this:
84+ 使用方法如下:
9085
9186.. code-block :: python
9287
9388 >> > for op, oparg in generate_opcodes(countdown.__code__ .co_code):
9489 ... print (op, opcode.opname[op], oparg)
9590
96- It’s a little-known fact, but you can replace the raw byte code of any function that you
97- want. It takes a bit of work to do it, but here’s an example of what’s involved:
91+ 这种方式很少有人知道,你可以利用它替换任何你想要替换的函数的原始字节码。
92+ 下面我们用一个示例来演示整个过程:
9893
9994.. code-block :: python
10095
@@ -120,10 +115,6 @@ want. It takes a bit of work to do it, but here’s an example of what’s invol
120115 >> > add(2 ,3 )
121116 Segmentation fault
122117
123- Having the interpreter crash is a pretty likely outcome of pulling a crazy stunt like this.
124- However, developers working on advanced optimization and metaprogramming tools
125- might be inclined to rewrite byte code for real. This last part illustrates how to do it. See
118+ 你可以像这样耍大招让解释器奔溃。但是,对于编写更高级优化和元编程工具的程序员来讲,
119+ 他们可能真的需要重写字节码。本节最后的部分演示了这个是怎样做到的。你还可以参考另外一个类似的例子:
126120`this code on ActiveState <http://code.activestate.com/recipes/277940-decorator-for-bindingconstants-at-compile-time/ >`_
127- for another example of such code in action.
128-
129-
0 commit comments