Private data

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Sun Mar 18 20:54:08 EDT 2007


En Sun, 18 Mar 2007 10:21:27 -0300, Dustan <DustanGroups at gmail.com>  
escribió:

>>>> dis.dis(testPrivateStaticFunctionVariables)
>  21           0 LOAD_DEREF               0 (func)
>               3 LOAD_DEREF               1 (internalData)
>               6 LOAD_FAST                0 (args)
>               9 CALL_FUNCTION_VAR        1
>              12 RETURN_VALUE
>
> What's the difference between 'LOAD_DEREF', 'LOAD_FAST', and
> 'LOAD_CONST', and, as seen at http://docs.python.org/lib/module-dis.html,
> 'LOAD_GLOBAL'? I can imagine that 'LOAD_GLOBAL' loads a global, but
> seeing as python is such a dynamic language, how exactly is it
> supposed to distinguish between them?

The Python interpreter executes a stack-based machine. Look at  
Python/compile.c for the code generation and Python/ceval.c for the code  
execution.
LOAD_CONST pushes a constant onto the stack (all constants are previously  
built and stored as attribute co_consts of the code object). LOAD_FAST and  
LOAD_GLOBAL pushes a local or global variable reference. (The compiler  
knows whether a name is local or not, just by lexical analysis).  
LOAD_DEREF appears to be used for accessing variables inside closures, I'm  
not sure.

> I don't understand the following at all: 'DUP_TOP', 'ROT_TWO'. Any
> pointers?

They manipulate the stack. DUP_TOP duplicates the top entry; ROT_TWO swaps  
the two top entries.

> What does 'INPLACE_ADD' mean, if not in place addition, and if it is
> in place addition, why does it need to 'STORE_ATTR' afterward?

That corresponds to A += B. The compiler can't know if A has or not an  
__iadd__ method; even if A implements __iadd__, that method could return a  
different object, not always A (it *may* try to do the operation inplace,  
but that might not always be possible). When the inplace method is not  
implemented, __add__ is used (so effectively A += B is computed as A = A+B)

-- 
Gabriel Genellina




More information about the Python-list mailing list