Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #104589 > unrolled thread
| Started by | "Charles T. Smith" <cts.private.yahoo@gmail.com> |
|---|---|
| First post | 2016-03-11 08:13 +0000 |
| Last post | 2016-03-11 03:20 -0600 |
| Articles | 8 — 5 participants |
Back to article view | Back to comp.lang.python
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
| From | "Charles T. Smith" <cts.private.yahoo@gmail.com> |
|---|---|
| Date | 2016-03-11 08:13 +0000 |
| Subject | a clarification on the "global" statement sought |
| Message-ID | <nbtunf$u4r$1@dont-email.me> |
When might a "global" statement be used in the outermost level of a module? (whereby, I assume a module is equivalent to a python file, correct?) TIA for any thoughts. cts
[toc] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2016-03-11 19:29 +1100 |
| Message-ID | <mailman.3.1457684962.26429.python-list@python.org> |
| In reply to | #104589 |
On Fri, Mar 11, 2016 at 7:13 PM, Charles T. Smith <cts.private.yahoo@gmail.com> wrote: > When might a "global" statement be used in the outermost level of a module? > > (whereby, I assume a module is equivalent to a python file, correct?) > Usefully? Never. Simple question - simple answer :) ChrisA
[toc] | [prev] | [next] | [standalone]
| From | "Charles T. Smith" <cts.private.yahoo@gmail.com> |
|---|---|
| Date | 2016-03-11 08:53 +0000 |
| Message-ID | <nbu12v$u4r$2@dont-email.me> |
| In reply to | #104591 |
On Fri, 11 Mar 2016 19:29:20 +1100, Chris Angelico wrote: > Usefully? Never. > > Simple question - simple answer :) > > ChrisA Right, that was the expected answer as well. I just ran into that in legacy code, checked out the documentation and couldn't really make that out. So I figured I better ask here before I end up with egg on my face :) Thanks folks. Actually, it would be nice if there were a -lint or -warnings flag for such cases, a la perl or gcc.
[toc] | [prev] | [next] | [standalone]
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2016-03-11 10:23 +0100 |
| Message-ID | <mailman.9.1457688217.26429.python-list@python.org> |
| In reply to | #104594 |
Charles T. Smith wrote: > On Fri, 11 Mar 2016 19:29:20 +1100, Chris Angelico wrote: > >> Usefully? Never. >> >> Simple question - simple answer :) >> >> ChrisA > > > Right, that was the expected answer as well. I just ran into that in > legacy code, checked out the documentation and couldn't really make that > out. So I figured I better ask here before I end up with egg on my face > > :) > > Thanks folks. > > Actually, it would be nice if there were a -lint or -warnings flag > for such cases, a la perl or gcc. There is an external tool called pylint that among other things detects superfluous global statements.
[toc] | [prev] | [next] | [standalone]
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Date | 2016-03-11 08:31 +0000 |
| Message-ID | <mailman.4.1457685127.26429.python-list@python.org> |
| In reply to | #104589 |
On 11/03/2016 08:13, Charles T. Smith wrote: > When might a "global" statement be used in the outermost level of a module? Never. Hopefully this http://www.python-course.eu/python3_global_vs_local_variables.php can explain it better than I can :) > > (whereby, I assume a module is equivalent to a python file, correct?) Correct. > > TIA for any thoughts. > > cts > -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence
[toc] | [prev] | [next] | [standalone]
| From | "Charles T. Smith" <cts.private.yahoo@gmail.com> |
|---|---|
| Date | 2016-03-11 09:09 +0000 |
| Message-ID | <nbu1vv$u4r$3@dont-email.me> |
| In reply to | #104592 |
On Fri, 11 Mar 2016 08:31:22 +0000, Mark Lawrence wrote:
> Never. Hopefully this
> http://www.python-course.eu/python3_global_vs_local_variables.php can
> explain it better than I can :)
The article is good, I'm glad to have confirmed what I have so empirical
stumbled over.
... Irrespective of the "python-speak" (or to be fair,
evangelical-speak):
"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.
Nevertheless, python is still a fun language to program in.
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2016-03-11 20:29 +1100 |
| Message-ID | <mailman.11.1457688596.26429.python-list@python.org> |
| In reply to | #104597 |
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
[toc] | [prev] | [next] | [standalone]
| From | eryk sun <eryksun@gmail.com> |
|---|---|
| Date | 2016-03-11 03:20 -0600 |
| Message-ID | <mailman.8.1457688076.26429.python-list@python.org> |
| In reply to | #104589 |
On Fri, Mar 11, 2016 at 2:13 AM, Charles T. Smith
<cts.private.yahoo@gmail.com> wrote:
> When might a "global" statement be used in the outermost level of a module?
You wouldn't need this in a normal module, because locals and globals
are the same. It may be useful if you're using exec with separate
locals and globals, and need to set a global. For example:
source = 'global y; x, y = 1, 2'
gvars, lvars = {}, {}
exec(source, gvars, lvars)
>>> lvars
{'x': 1}
>>> gvars['y']
2
Probably you'll never need this.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web