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


Groups > comp.lang.python > #54577

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

From Peter Otten <__peter__@web.de>
Subject Re: Why do I have to use "global" so much when using Turtle?
Date 2013-09-22 10:15 +0200
Organization None
References <0b028e65-37b2-492a-83c0-0d76d037c165@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.239.1379837679.18130.python-list@python.org> (permalink)

Show all headers | View raw


John Ladasky wrote:

> Hi, folks,
> 
> Some of you may remember that I am teaching some high school students how
> to program.  Because they all love graphics, I have been investigating the
> turtle module, which I gather is built on top of Tk.  I can see that
> real-time applications are possible.  I'm writing a classic
> "bouncing-balls" program to demonstrate to my students.
> 
> In the main program code, I instantiate a turtle.Screen, named sc.
> 
> I draw a bounding box, whose size is described by the variables edgew and
> edgeh.  I have a list of Turtle objects, which I named balls.  I have a
> timer interval stored in tick.
> 
> In the main loop of my program, I bind a function to update the positions
> of the balls to a timer, thus:
> 
> sc.ontimer(move_balls, tick)
> 
> Inside my move_balls function, I could not get ANYTHING done without the
> following declaration:
> 
> global balls, edgew, edgeh, tick

That's not limited to turtle, Python in general forces you to make shared 
state explicit. 

Simple alternatives to global would be a degenerate class

class state:
    pass

or a magic import

import __main__ as state

With both of these (pseudo-)globals can be set with

state.some_global = some_value

However, most of the names you list above look like their values need not be 
altered from within the function and thus the name not be declared global 
anyway. To be sure you'd have to show us the code...


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