Lexical Scope

Discussion in 'Python' started by Matt Knepley, Oct 30, 2003.

  1. Matt Knepley

    Matt Knepley Guest

    I must be misunderstanding how Python 2.3 handles lexical scoping.
    Here is a sample piece of code:

    def run():
    a = 1
    def run2(b):
    print a
    run2(2)
    print a
    run()

    which gives the output:

    1
    1

    whereas this piece of code:

    def run():
    a = 1
    def run2(b):
    a = b
    print a
    run2(2)
    print a
    run()

    gives:

    2
    1

    and finally this code bombs:

    def run():
    a = 1
    def run2(b):
    print a
    a = b
    run2(2)
    print a
    run()

    with an error about UnboundLocal. It seems that lexical scope works
    only for references, and as soon as I make an assignment a new local
    is created. Is this true?

    Matt
    Matt Knepley, Oct 30, 2003
    #1
    1. Advertising

  2. Matt Knepley

    Gary Herron Guest

    On Thursday 30 October 2003 07:59 am, Matt Knepley wrote:
    > I must be misunderstanding how Python 2.3 handles lexical scoping.
    > Here is a sample piece of code:


    The rule is this simple:

    An assignment to a variable *ANYWHERE* within a block of code makes
    that variable local *EVERYWHERE* within that block, possibly hiding
    variables of the same name in outer scopes.

    That rule will explain all three of your examples.

    Gary Herron



    >
    > def run():
    > a = 1
    > def run2(b):
    > print a
    > run2(2)
    > print a
    > run()
    >
    > which gives the output:
    >
    > 1
    > 1
    >
    > whereas this piece of code:
    >
    > def run():
    > a = 1
    > def run2(b):
    > a = b
    > print a
    > run2(2)
    > print a
    > run()
    >
    > gives:
    >
    > 2
    > 1
    >
    > and finally this code bombs:
    >
    > def run():
    > a = 1
    > def run2(b):
    > print a
    > a = b
    > run2(2)
    > print a
    > run()
    >
    > with an error about UnboundLocal. It seems that lexical scope works
    > only for references, and as soon as I make an assignment a new local
    > is created. Is this true?
    >
    > Matt
    Gary Herron, Oct 30, 2003
    #2
    1. Advertising

  3. Hi,

    Matt Knepley wrote:

    >
    > It seems that lexical scope works
    > only for references, and as soon as I make an assignment a new local
    > is created. Is this true?
    >


    Yes (short answer)


    If you really need to modify some variable in an outer scope you can
    use a mutable object for that kind of thing, like so:

    >>> def run():

    .... a = [1]
    .... def run2(b):
    .... print a[0]
    .... a[0] = b
    .... run2(2)
    .... print a[0]
    ....
    >>> run()

    1
    2

    Or you can use a global variable like so:

    >>> def run():

    .... global a
    .... a = 1
    .... def run2(b):
    .... global a
    .... print a
    .... a = b
    .... run2(2)
    .... print a
    ....
    >>> run()

    1
    2


    It depends on what you are trying to accomplish :)


    hth

    Werner
    Werner Schiendl, Oct 30, 2003
    #3
  4. Matt Knepley

    Paul Clinch Guest

    From the language lawyers section of python doc.s :-

    "If a name is assigned to anywhere in a code block (even in
    unreachable code), and is not mentioned in a global statement in that
    code block, then it refers to a local name throughout that code
    block."

    Try:-

    def run():
    a = 1
    def run2(b):
    global a
    print a
    a = b
    run2(2)
    print a
    run()
    Paul Clinch, Oct 30, 2003
    #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. Replies:
    18
    Views:
    489
    Bengt Richter
    Dec 17, 2005
  2. globalrev

    python: lexical or dynamic scope?

    globalrev, May 13, 2008, in forum: Python
    Replies:
    3
    Views:
    869
    Mark Wooding
    May 14, 2008
  3. mrstevegross

    Weird behavior with lexical scope

    mrstevegross, Nov 6, 2008, in forum: Python
    Replies:
    8
    Views:
    244
    Lawrence D'Oliveiro
    Nov 7, 2008
  4. Xah Lee
    Replies:
    0
    Views:
    2,222
    Xah Lee
    Feb 26, 2009
  5. Andrew Savige

    Lexical scope: converting Perl to Python

    Andrew Savige, Jun 13, 2009, in forum: Python
    Replies:
    3
    Views:
    436
    John S
    Jun 13, 2009
Loading...

Share This Page