You can use a global variable within other functions by declaring it as global within each function that assigns a value to it:
globvar = 0
def set_globvar_to_one():
global globvar # Needed to modify global copy of globvar
globvar = 1
def print_globvar():
print(globvar) # No need for global declaration to read value of globvar
set_globvar_to_one()
print_globvar() # Prints 1
Since it's unclear whether globvar = 1 is creating a local variable or changing a global variable, Python defaults to creating a local variable, and makes you explicitly choose the other behavior with the global keyword.
See other answers if you want to share a global variable across modules.
Answer from Paul Stephenson on Stack OverflowYou can use a global variable within other functions by declaring it as global within each function that assigns a value to it:
globvar = 0
def set_globvar_to_one():
global globvar # Needed to modify global copy of globvar
globvar = 1
def print_globvar():
print(globvar) # No need for global declaration to read value of globvar
set_globvar_to_one()
print_globvar() # Prints 1
Since it's unclear whether globvar = 1 is creating a local variable or changing a global variable, Python defaults to creating a local variable, and makes you explicitly choose the other behavior with the global keyword.
See other answers if you want to share a global variable across modules.
If I'm understanding your situation correctly, what you're seeing is the result of how Python handles local (function) and global (module) namespaces.
Say you've got a module like this:
# sample.py
_my_global = 5
def func1():
_my_global = 42
def func2():
print _my_global
func1()
func2()
You might be expecting this to print 42, but instead, it prints 5. As has already been mentioned, if you add a 'global' declaration to func1(), then func2() will print 42.
def func1():
global _my_global
_my_global = 42
What's going on here is that Python assumes that any name that is assigned to, anywhere within a function, is local to that function unless explicitly told otherwise. If it is only reading from a name, and the name doesn't exist locally, it will try to look up the name in any containing scopes (e.g. the module's global scope).
When you assign 42 to the name _my_global, therefore, Python creates a local variable that shadows the global variable of the same name. That local goes out of scope and is garbage-collected when func1() returns; meanwhile, func2() can never see anything other than the (unmodified) global name. Note that this namespace decision happens at compile time, not at runtime -- if you were to read the value of _my_global inside func1() before you assign to it, you'd get an UnboundLocalError, because Python has already decided that it must be a local variable but it has not had any value associated with it yet. But by using the 'global' statement, you tell Python that it should look elsewhere for the name instead of assigning to it locally.
(I believe that this behavior originated largely through optimization of local namespaces -- without this behavior, Python's VM would need to perform at least three name lookups each time a new name is assigned to inside a function (to ensure that the name didn't already exist at module/builtin level), which would significantly slow down a very common operation.)
How to make variables global by default, Python 3.11
How to declare a global variable in Python correctly - Stack Overflow
How to handle 'global' variables?
Is there a way to make a variable global to all functions?
Videos
Recently, my teacher and I had some problems about this particular lesson that could not be solve because of both of us being stubborn. Note that this is a beginner class for programmers and the teacher was using a website w3schools to teach in his lesson. When he was revising the lesson in Python Variables, he asked "Create a global variable". Many students rose their hand then he decided to choose me. I went up to the board and wrote var1 = "hello, world!". You might say this is a very simple and easy line of code. I was sure this couldn't be wrong since I knew the definition global variable and that is variables that are created outside of a function (as in all of the examples in the previous pages in w3schools) are known as global variables. This definition tought me that I didn't need to make a function (like local variables) to make a global variable. Then afterwards he decided to mock me saying after I was wrong. Different from everyone else. I was wrong that my code needed more line. Specificly a function and an output to consider it as a global variable. This situation escalated to threatening me to the principle and calling about another teacher to prove me wrong. Ofcourse I did respond to this matter back respectfully but he couldn't agree with me. I was trying act like I was not mad to be respectful and not use any informal behaviour. Though I am starting to be annoyed of this. After all, he did told be to do my research to find "a professor" to proof me wrong, etc. I decided to ask reddit if I am truly in the wrong or not and to see any other opinions of my fellow reddit users about this matter. And if I am right, this reddit might be use to prove the teacher to getting my "deserving points" back from a complex misunderstanding.
Edit: var1 = "hello, world!" is written completely by itself and not inside of anything. And the teacher specificly said "Create a global variable" so that mean no function was mention. I hope this could give out some more ideas about the perspective. And as mention the class have only learnt to python variables so as of reading the comments, I couldn't even understand what some of the lines of code you guys wrote.
You "declare" it outside the the functions as you did here. But you're missing the global keyword.
Then:
If you only read its value inside a function, it's treated as a global variable.
variable_name = "old value" def function(): print(variable_name) function() # old value print(variable_name) # old valueNormally, if you write to it, it's treated as a local variable.
variable_name = "old value" def function(): variable_name = "new value" print(variable_name) function() # new value print(variable_name) # old valueTo write to it as a global variable, add the
globalkeyword in the function definition before using the variable.variable_name = "old value" def function(): global variable_name variable_name = "new value" print(variable_name) function() # new value print(variable_name) # new value
Source: https://docs.python.org/faq/programming.html#what-are-the-rules-for-local-and-global-variables-in-python
If a function assigns to a variable name (which your greet() function does for the player variable), then that variable name is treated as local everywhere in the function, even if there is a global variable of the same name.
If you want the function to use the global variable, put this at the top of the function:
global player
By contrast, your print_board() function does not assign to a variable named board, therefore it is able to use that global variable without explicitly declaring global board.
Fairly new to Python, is there a way to make a variable global to all functions (so I can edit them) instead of having to use the global keyword in every single function.