How does this code works:

Discussion in 'Python' started by vch, Jul 11, 2005.

  1. vch

    vch Guest

    Here's an example from some book:

    def foo(n):
    s = [n]
    def bar(i):
    s[0] += i
    return s[0]
    return bar

    what I don't understand is how this example works, taking into account
    the LGB rule. I thought that s is not accessible from bar, but it is,
    apparently. Why?
    vch, Jul 11, 2005
    #1
    1. Advertising

  2. vch wrote:
    > Here's an example from some book:
    >
    > def foo(n):
    > s = [n]
    > def bar(i):
    > s[0] += i
    > return s[0]
    > return bar
    >
    > what I don't understand is how this example works, taking into account
    > the LGB rule. I thought that s is not accessible from bar, but it is,
    > apparently. Why?


    Nested Scopes (Since Python 2.1):

    http://www.python.org/peps/pep-0227.html
    Rocco Moretti, Jul 12, 2005
    #2
    1. Advertising

  3. vch

    Robert Kern Guest

    vch wrote:
    > Here's an example from some book:
    >
    > def foo(n):
    > s = [n]
    > def bar(i):
    > s[0] += i
    > return s[0]
    > return bar
    >
    > what I don't understand is how this example works, taking into account
    > the LGB rule. I thought that s is not accessible from bar, but it is,
    > apparently. Why?


    Python now has nested scopes.

    http://www.python.org/peps/pep-0227.html

    --
    Robert Kern


    "In the fields of hell where the grass grows high
    Are the graves of dreams allowed to die."
    -- Richard Harter
    Robert Kern, Jul 12, 2005
    #3
    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. F. GEIGER
    Replies:
    3
    Views:
    763
    F. GEIGER
    Aug 6, 2004
  2. Bo Yang

    How does these code works ?

    Bo Yang, Oct 16, 2006, in forum: C++
    Replies:
    7
    Views:
    323
    Alf P. Steinbach
    Oct 16, 2006
  3. Alexander Burger

    getMethod() works and works not

    Alexander Burger, Nov 27, 2010, in forum: Java
    Replies:
    25
    Views:
    1,744
    Alexander Burger
    Nov 29, 2010
  4. abargaddon
    Replies:
    1
    Views:
    192
    clintmazur
    Feb 4, 2008
  5. Phi!
    Replies:
    1
    Views:
    177
Loading...

Share This Page