Movatterモバイル変換
[0]ホーム
Confused about nested scoping
Brian QuinlanBrianQ at ActiveState.com
Wed Apr 18 19:35:53 EDT 2001
The problem is that "printspam" is not inside "scopetest"'s scope.Try this example:test = 'Global'def a(): test = 'Local to "a"' def b(): print test b()a() # "Global"and:from __future__ import nested_scopestest = 'Global'def a(): test = 'Local to "a"' def b(): print test b()a() # 'Local to "a"'> -----Original Message-----> From:python-list-admin at python.org> [mailto:python-list-admin at python.org]On Behalf Of Stephen R. Figgins> Sent: Wednesday, April 18, 2001 4:18 PM> To:python-list at python.org> Subject: Confused about nested scoping>>> I seem to be misunderstanding what nested scopes do.>> >From AMK's summary of 2.1 changes:>> Put simply, when a given variable name is not assigned a value> within a function (by an assignment, or the def, class, or import> statements), references to the variable will be looked up in the> local namespace of the enclosing scope.>> Here is what I tried:>> from __future__ import nested_scopes>> def printspam():> print "spam has value %s" % (spam)>> def scopetest():> spam = 'bacon'> printspam()>> spam = 'eggs'> printspam()> scopetest()>>> I figured without nested scopes I would get>> spam has value eggs> spam has value eggs>> But I thought with nested scopes I would get>> spam has value eggs> spam has value bacon>> Because the second printspam's enclosing scope would be that of> scopetest in which I had reassigned spam.>> But I still get eggs with my spam.>> What am I misunderstanding?>> -Stephen> -->http://mail.python.org/mailman/listinfo/python-list>
More information about the Python-listmailing list
[8]ページ先頭