Added a bit about global variables
This commit is contained in:
		
							parent
							
								
									821773a037
								
							
						
					
					
						commit
						2e938a6bb8
					
				
							
								
								
									
										34
									
								
								Python/002_globals.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										34
									
								
								Python/002_globals.py
									
									
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,34 @@
 | 
				
			||||||
 | 
					# Global Scope
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# We will discuss scope in the context of functions and classes
 | 
				
			||||||
 | 
					# at a later point.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# More detailed reading: https://realpython.com/python-constants/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# The "scope" of anything (variable, function, etc.) means
 | 
				
			||||||
 | 
					# "where is this thing available".
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# For this initial introduction we will only be discussing
 | 
				
			||||||
 | 
					# the "global scope". Global means "available everywhere".
 | 
				
			||||||
 | 
					# Anything defined in the global scope can be accessed in
 | 
				
			||||||
 | 
					# the main part of the program, inside the body/blocks of
 | 
				
			||||||
 | 
					# a function, and from within classes.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Any variable declared in the top part of the program is
 | 
				
			||||||
 | 
					# technically a global variable and can be accessed anywhere.
 | 
				
			||||||
 | 
					# However, it is (usually) considered bad form to change global
 | 
				
			||||||
 | 
					# variable values inside functions and classes.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# We use a special notation to define global variables which
 | 
				
			||||||
 | 
					# are intended to hold values for settings and such.
 | 
				
			||||||
 | 
					# Create them using all capital letters. Visual Studio Code
 | 
				
			||||||
 | 
					# will actually color these slighly differently so it is obvious.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					MAX_LOOPS = 500
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					loop_count = 0
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Notice that these two variables are slightly different colors.
 | 
				
			||||||
 | 
					# Nothing prevents you from changing the value of an all upper
 | 
				
			||||||
 | 
					# case variable, but the convention of using all caps is meant
 | 
				
			||||||
 | 
					# to indicate that the value SHOULDN'T be changed.
 | 
				
			||||||
		Loading…
	
		Reference in New Issue
	
	Block a user