lambda in list comprehension acting funny

Hans Mulder hansmu at xs4all.nl
Sun Jul 15 21:25:57 CEST 2012


On 15/07/12 10:44:09, Chris Angelico wrote:
> On Sun, Jul 15, 2012 at 6:32 PM, Steven D'Aprano
> <steve+comp.lang.python at pearwood.info> wrote:
>> At compile time, Python parses the source code and turns it into byte-
>> code. Class and function definitions are executed at run time, the same
>> as any other statement.
> 
> Between the parse step and the 'def' execution, a code object is
> created. When you call it, that code object already exists. Nothing
> else really matters, unless there's a bug in the Python optimizer or
> something weird like that. The nearest thing Python _has_ to a
> "compile time" is the execution of def.
> 
> ChrisA

"Compile time" is the phase when your Python code is turned into byte
code, or a SyntaxError is raised.  In this phase, a "code object" is
created for the function.

"Function definition time" is when the "def" command is executed.
In this phase, default arguments are computed, and a "function object"
is created.  Among the attributes of the function object are the code
object, and "cell" objects containing the bindings for its non-local
variables.  These bindings are used to read the variable's current
value at the time the function uses the variable.


-- HansM



More information about the Python-list mailing list