In [13]: import dis
In [15]: def foo():
....: a = 10
In [16]: dis.dis(foo)
2 0 LOAD_CONST 1 (10)
3 STORE_FAST 0 (a)
6 LOAD_CONST 0 (None)
9 RETURN_VALUE
The STORE_FAST instruction is storing the (reference to) the value 10 at
offset 0 in the local storage section of the stack frame.
I looked up "offset" in Wikipedia:
====================================================
Offset (computer science)
This article describes the computer science term.
In computer science, an offset within an array or other data structure object is an integer indicating the distance (displacement) from the beginning of the object up until a given element or point, presumably within the same object. The concept of a distance is valid only if all elements of the object are the same size (typically given in bytes or words).
In computer engineering and low-level programming (such as assembly language), an offset usually denotes the number of address locations added to a base address in order to get to a specific absolute address. In this meaning of offset, only the basic address unit, usually the 8-bit byte, is used to specify the offset's size. In this context an offset is sometimes called a relative address.
For example, given an array of characters A, containing abcdef, one can say that the element containing the letter 'c' has an offset of 2 from the start of A.
Retrieved from " http://en.wikipedia.org/wiki/Offset_%28computer_science%29"
===========================================================
This may be pushing it with you, but I found the dis.dis() thing fascinating. Here's a bit more complex function.
def f(x):
if x%2:
return "odd"
else:
return "even"
Could you give a blow-by-blow on the dis.dis()?
=======================================
In [21]: import dis
....:
Out[21]: <module 'dis' from 'E:\Python25\lib\dis.pyc'>
In [22]: def f(x):
....: if x%2:
....: return "odd"
....: else:
....: return "even"
....:
....:
In [23]: dis.dis(f)
2 0 LOAD_FAST 0 (x)
3 LOAD_CONST 1 (2)
6 BINARY_MODULO
7 JUMP_IF_FALSE 8 (to 18)
10 POP_TOP
3 11 LOAD_CONST 2 ('odd')
14 RETURN_VALUE
15 JUMP_FORWARD 5 (to 23)
>> 18 POP_TOP
5 19 LOAD_CONST 3 ('even')
22 RETURN_VALUE
>> 23 LOAD_CONST 0 (None)
26 RETURN_VALUE
=========================================
Thanks,
Dick Moores
UliPad <<The Python Editor>>: http://code.google.com/p/ulipad/
_______________________________________________ Tutor maillist - [email protected] http://mail.python.org/mailman/listinfo/tutor
