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

Ned Batchelder ned at nedbatchelder.com
Sun Sep 22 06:56:02 EDT 2013


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.



More information about the Python-list mailing list