Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #54582

Re: Why do I have to use "global" so much when using Turtle?

Date 2013-09-22 06:56 -0400
From Ned Batchelder <ned@nedbatchelder.com>
Subject Re: Why do I have to use "global" so much when using Turtle?
References <0b028e65-37b2-492a-83c0-0d76d037c165@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.242.1379847370.18130.python-list@python.org> (permalink)

Show all headers | View raw


On 9/22/13 12:39 AM, John Ladasky wrote:
> For a while, I had a "quit" function that I bound to the q key:
>
> sc.onkeypress(quit, "q")
>
> The quit function simply printed a message, and then called sc.bye().  As with move_balls, quit wouldn't work unless I had a "global sc" declaration in it.  (My shortened program skips my function and just binds sc.bye to the q key, avoiding the need for globals.)

It sounds like you didn't need as many global statements as you think.  
In particular, you only need to use "global" if you are assigning to a 
global name within a function.  The quit function you describe didn't 
have a "sc = ..." statement in it, and so should not have needed a 
"global sc" statement.

Can you show the code with the global statements?  There's very likely a 
way to get rid of some of them.

--Ned.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Why do I have to use "global" so much when using Turtle? John Ladasky <john_ladasky@sbcglobal.net> - 2013-09-21 21:39 -0700
  Re: Why do I have to use "global" so much when using Turtle? Peter Otten <__peter__@web.de> - 2013-09-22 10:15 +0200
  Re: Why do I have to use "global" so much when using Turtle? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-09-22 10:43 +0000
  Re: Why do I have to use "global" so much when using Turtle? Ned Batchelder <ned@nedbatchelder.com> - 2013-09-22 06:56 -0400
  Re: Why do I have to use "global" so much when using Turtle? Nobody <nobody@nowhere.com> - 2013-09-22 14:48 +0100
  Re: Why do I have to use "global" so much when using Turtle? John Ladasky <john_ladasky@sbcglobal.net> - 2013-09-22 11:57 -0700
    Re: Why do I have to use "global" so much when using Turtle? rusi <rustompmody@gmail.com> - 2013-09-22 18:44 -0700
      Re: Why do I have to use "global" so much when using Turtle? John Ladasky <john_ladasky@sbcglobal.net> - 2013-09-23 00:26 -0700

csiph-web