473,396 Members | 2,093 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Global variables in function object

7
The following code is from the turtle graphics demos. The program is a color mixer and allows the user to change the background color of the window using three turtle sliders.

Under the function definition "main" there are three global variables defined ("screen", "red", and "blue").

I'm trying to understand the purpose of defining these variables as global within the function definition.

Expand|Select|Wrap|Line Numbers
  1.  
  2. from turtle import Screen, Turtle, mainloop
  3.  
  4. class ColorTurtle(Turtle):
  5.  
  6.     def __init__(self, x, y):
  7.         Turtle.__init__(self)
  8.         self.shape("turtle")
  9.         self.resizemode("user")
  10.         self.shapesize(3,3,5)
  11.         self.pensize(10)
  12.         self._color = [0,0,0]
  13.         self.x = x
  14.         self._color[x] = y
  15.         self.color(self._color)
  16.         self.speed(0)
  17.         self.left(90)
  18.         self.pu()
  19.         self.goto(x,0)
  20.         self.pd()
  21.         self.sety(1)
  22.         self.pu()
  23.         self.sety(y)
  24.         self.pencolor("gray25")
  25.         self.ondrag(self.shift)
  26.  
  27.     def shift(self, x, y):
  28.         self.sety(max(0,min(y,1)))
  29.         self._color[self.x] = self.ycor()
  30.         self.fillcolor(self._color)
  31.         setbgcolor()
  32.  
  33. def setbgcolor():
  34.     screen.bgcolor(red.ycor(), green.ycor(), blue.ycor())
  35.  
  36. def main():
  37.     global screen, red, green, blue
  38.     screen = Screen()
  39.     screen.delay(0)
  40.     screen.setworldcoordinates(-1, -0.3, 3, 1.3)
  41.  
  42.     red = ColorTurtle(0, .5)
  43.     green = ColorTurtle(1, .5)
  44.     blue = ColorTurtle(2, .5)
  45.     setbgcolor()
  46.  
  47.     writer = Turtle()
  48.     writer.ht()
  49.     writer.pu()
  50.     writer.goto(1,1.15)
  51.     writer.write("DRAG!",align="center",font=("Arial",30,("bold","italic")))
  52.     return "EVENTLOOP"
  53.  
  54. if __name__ == "__main__":
  55.     msg = main()
  56.     print(msg)
  57.     mainloop()
  58.  
  59.  
Nov 25 '18 #1
0 940

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Matt | last post by:
Greetings, What are people's thoughts on global variables in C++? Why are we taught not to use them in programming? Is it true that if you are running two copies of the C program one copy can...
5
by: Richard A. DeVenezia | last post by:
Dear Experts: Suppose I have global variables: x1, x2, x3 and I have a function that needs to assign a value to a new global variable x4 something like function foo () { count = 0;
8
by: lawrence | last post by:
I'm learning Javascript. I downloaded a script for study. Please tell me how the variable "loop" can have scope in the first function when it is altered in the second function? It is not defined...
17
by: MLH | last post by:
A97 Topic: If there is a way to preserve the values assigned to global variables when an untrapped runtime error occurs? I don't think there is, but I thought I'd ask. During development, I'm...
7
by: Michael | last post by:
Hi newsgroup, as the subject indicates I am looking for an advice using global variables. I am not if this problem is more about style then C. If its wrong in thi group, sorry. So I have a...
10
by: ankisharma | last post by:
Hi all At many places I have seen that programmers pass global variables to functions in c. I am not able to figure out why they do so. need some clues on this. somewhere i heard that this...
8
by: newbie | last post by:
Hello, I have questions about global variables in OOP (in general) and Python (in specific). I understand (I think) that global variables are generally not a good idea. However, if there are...
10
by: Jay Wolfe | last post by:
Hello, I'm trying to make sure I use best practices (and hence save myself some headaches) with the declaration and definition of global variables. Let's say I have an app with 30 files,...
9
by: CDMAPoster | last post by:
About a year ago there was a thread about the use of global variables in A97: http://groups.google.com/group/comp.databases.ms-access/browse_frm/thread/fedc837a5aeb6157 Best Practices by Kang...
1
weaknessforcats
by: weaknessforcats | last post by:
C++: The Case Against Global Variables Summary This article explores the negative ramifications of using global variables. The use of global variables is such a problem that C++ architects have...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.