strange SyntaxError

Discussion in 'Python' started by Attila Szabo, Feb 25, 2005.

  1. Attila Szabo

    Attila Szabo Guest

    Hi,

    I wrote this sample piece of code:

    def main():
    lambda x: 'ABC%s' % str(x)
    for k in range(2): exec('print %s' % k)

    main()

    With the lambda line, I get this:
    SyntaxError: unqualified exec is not allowed in function 'main'
    it contains a nested function with free variables
    Without the lambda, it's ok...

    What's this ?

    thanks

    --

    A t t i l a :: :: S z a b o
     
    Attila Szabo, Feb 25, 2005
    #1
    1. Advertising

  2. Attila Szabo wrote:
    > Hi,
    > def main():
    > lambda x: 'ABC%s' % str(x)
    > for k in range(2): exec('print %s' % k)

    OK, to no real effect, in main you define an unnamed function that
    you can never reference. Pretty silly, but I'll bite.

    Next you run run a loop with exec looking like you think it is a
    function. In the loop, your "exec" statement does not control
    (specify) its access to global and local variables. This could be
    a problem if your function were different. The function above does
    not do any up-referencing, but:

    def main():
    def inner():
    return k + 1

    for k in range(4):
    exec 'k = 3 * k'
    print k, inner()

    What should this do?

    If you think you know, how about replacing the exec line with:

    exec raw_input('Tricky: ')

    You can fix this by controlling what variables the exec can write:

    exec 'k = 3 * k' in globals(), locals()

    This is why locals() does not do write-back.

    The rule below (which was high on the list when searching for exec),
    tells the exact rules, the above stuff is just why.

    http://www.python.org/doc/2.4/ref/dynamic-features.html

    --Scott David Daniels
     
    Scott David Daniels, Feb 25, 2005
    #2
    1. Advertising

  3. Attila Szabo

    Attila Szabo Guest

    2005, Feb 25 -> Scott David Daniels wrote :
    > Attila Szabo wrote:
    > >Hi,
    > >def main():
    > > lambda x: 'ABC%s' % str(x)
    > > for k in range(2): exec('print %s' % k)

    > OK, to no real effect, in main you define an unnamed function that
    > you can never reference. Pretty silly, but I'll bite.


    This code was simplified, the lambda was part of a map,
    and the exec line is different too, I just wrote
    this to picture the situation, but it's ok now...
    At first I was surprised, but I read a bit and it's clear now...

    Thanks
    --

    A t t i l a :: :: S z a b o
     
    Attila Szabo, Feb 26, 2005
    #3
  4. Attila Szabo wrote:
    > 2005, Feb 25 -> Scott David Daniels wrote :
    >>Attila Szabo wrote:
    >>... lambda x: 'ABC%s' % str(x) ...
    >>OK, to no real effect, in main you define an unnamed function that
    >>you can never reference. Pretty silly, but I'll bite.


    > This code was simplified, the lambda was part of a map, ...


    I'm sorry, upon re-reading my reply, I see how it looks like a
    snide comment. I apologize. I didn't mean to malign you, and
    I did indeed appreciate that the code was obviously boiled down.
    I just thought:
    def named(x):
    return 'ABC%s' % str(x)
    provides a function name to talk about, exhibits the same problem,
    and is "simpler".

    --Scott David Daniels
     
    Scott David Daniels, Feb 26, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. j vickroy
    Replies:
    1
    Views:
    1,173
    j vickroy
    Oct 8, 2003
  2. Lonnie Princehouse
    Replies:
    11
    Views:
    675
    Steve Holden
    Sep 15, 2005
  3. Todd
    Replies:
    1
    Views:
    315
    Erik Max Francis
    Dec 23, 2005
  4. DIBS

    SyntaxError: invalid syntax

    DIBS, Dec 27, 2005, in forum: Python
    Replies:
    5
    Views:
    39,445
    Terry Hancock
    Dec 27, 2005
  5. CoolGenie

    a strange SyntaxError

    CoolGenie, Dec 9, 2007, in forum: Python
    Replies:
    5
    Views:
    366
    Peter Otten
    Dec 10, 2007
Loading...

Share This Page