Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #104603
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: a clarification on the "global" statement sought |
| Date | 2016-03-11 20:29 +1100 |
| Message-ID | <mailman.11.1457688596.26429.python-list@python.org> (permalink) |
| References | <nbtunf$u4r$1@dont-email.me> <mailman.4.1457685127.26429.python-list@python.org> <nbu1vv$u4r$3@dont-email.me> |
On Fri, Mar 11, 2016 at 8:09 PM, Charles T. Smith <cts.private.yahoo@gmail.com> wrote: > "Python deals with variables the other way around. > They are local, if not otherwise declared. > ... > def f(): > print(s) > s = "I love Paris in the summer!" > f() > ... > As there is no local variable s, i.e. no assignment to s, the value > from the ***global*** variable s will be used." > > Indeed "maverick": that a variable can be an undefined global > and then only appears as such when assigned to, has caused me > no end of grief. Looking purely at the function definition, you can see with 100% certainty that it references two non-local names: "print" and "s". Neither is assigned to within the function, so both are looked up externally. At run time, s is resolved as a module-level name; print is not, so Python looks further, to the builtins. Had s not been assigned to, it would still be a global name in the function, but it would fail to be found in either the module namespace or the builtins, and would result in NameError. But either way, it's a global name, whether it's assigned to or not. ChrisA
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
a clarification on the "global" statement sought "Charles T. Smith" <cts.private.yahoo@gmail.com> - 2016-03-11 08:13 +0000
Re: a clarification on the "global" statement sought Chris Angelico <rosuav@gmail.com> - 2016-03-11 19:29 +1100
Re: a clarification on the "global" statement sought "Charles T. Smith" <cts.private.yahoo@gmail.com> - 2016-03-11 08:53 +0000
Re: a clarification on the "global" statement sought Peter Otten <__peter__@web.de> - 2016-03-11 10:23 +0100
Re: a clarification on the "global" statement sought Mark Lawrence <breamoreboy@yahoo.co.uk> - 2016-03-11 08:31 +0000
Re: a clarification on the "global" statement sought "Charles T. Smith" <cts.private.yahoo@gmail.com> - 2016-03-11 09:09 +0000
Re: a clarification on the "global" statement sought Chris Angelico <rosuav@gmail.com> - 2016-03-11 20:29 +1100
Re: a clarification on the "global" statement sought eryk sun <eryksun@gmail.com> - 2016-03-11 03:20 -0600
csiph-web