Re: variable declaration

From: Alex Martelli (aleaxit_at_yahoo.com)
Date: 02/08/05


Date: Tue, 8 Feb 2005 09:04:10 +0100

Terry Reedy <tjreedy@udel.edu> wrote:

> "Brian van den Broek" <bvande@po-box.mcgill.ca> wrote in message
> news:4208178F.40804@po-box.mcgill.ca...
> > Is the right way to understand it in this vicinity:
>
> In the vicinity, but not quite exact
>
> >At compile time (by which I mean when the Python bytecode is built)
>
> Compile time is when the def statement is executed

I disagree: compile time is when the compiler is running (for example,
the compiler is the component which diagnoses syntax errors, while other
errors are diagnosed ``at runtime'').

Bytecode is built before def executes -- indeed it's built whether def
executes or not. Cfr:

>>> def f(wo):
... if wo:
... def g(): pass
...
>>> f.func_code.co_consts
(None, <code object g at 0x382a60, file "<stdin>", line 3>)

the 'def g' hasn't executed (yet?), but the bytecode is built (and
stored as the first constantvalue in f, save the ubiquitous None).

If the ``if wo: def ...'' construct was in a module being imported, the
mechanics would be similar -- although in this case I think you could
tell only if a syntax error was present in the def statement (I may be
wrong, but offhand I don't think the codeobject is saved in this case).

Or, try this one:

>>> c = compile('def x(): pass', '<string>', 'exec')
>>> c
<code object ? at 0x389420, file "<string>", line 1>
>>> c.co_consts
(<code object x at 0x3893e0, file "<string>", line 1>, None)
>>>

``compile time'' here is when the 'compile' built-in runs; and we can
see the code object ``x'' is already built even though the def has not
been executed yet.

Alex



Relevant Pages

  • Re: self question
    ... As a result the compiler can evaluate 'foo' when it defines 'bar', ... The def statement is, as you say, an executable statement. ... current local variables which are in scope when the def executes. ...
    (comp.lang.python)
  • Re: converting some autogenerated ruby code to C
    ... faster than the pure Ruby version. ... I'm wondering how much optimization has been done there. ... def test ... You would either have to generate and compile and load a C extension for _each_ call of *eval or somehow collect all the code to eval and do it all at once. ...
    (comp.lang.ruby)
  • Re: securing a python execution environment...
    ... If Bad Stuffis not detected, then I just compile it with the ... def visitImport: ... raise ImportError, "The modules %s could not be ... to run Python code on your box, you're basically screwed, anyway. ...
    (comp.lang.python)
  • Using Rake to compile FreeBASIC code
    ... offer us native assembly-linked code that we ... currently use to drive a few special purpose dlls on windows. ... We solved this creating an Array of "MAINS", when the compile rule find ... def fbc_compile ...
    (comp.lang.ruby)
  • Re: doctest problem with null byte
    ... def quote_value: ... compile() expected string without null bytes ... doctest examples are parsed /twice/: ... Because it's a raw string, ...
    (comp.lang.python)