By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,696 Members | 2,213 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,696 IT Pros & Developers. It's quick & easy.

How does this code works:

P: n/a
vch
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?
Jul 21 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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
rk***@ucsd.edu

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter

Jul 21 '05 #2

P: n/a
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
Jul 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.