How to make a local variable (inside a function) global
Here are two methods to achieve the same thing:
Using parameters and return (recommended)
def other_function(parameter):
return parameter + 5
def main_function():
x = 10
print(x)
x = other_function(x)
print(x)
When you run main_function
, you'll get the following output>>> 10
>>> 15
Using globals (never do this)x = 0 # The initial value of x, with global scope
def other_function():
global x
x = x + 5
def main_function():
print(x) # Just printing - no need to declare global yet
global x # So we can change the global x
x = 10
print(x)
other_function()
print(x)
Now you will get:>>> 0 # Initial global value
>>> 10 # Now we've set it to 10 in `main_function()`
>>> 15 # Now we've added 5 in `other_function()`
Using global variables in a function
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.
How to declare global variable inside function?
You have two problems:
main
is not a loop. It's a function.Your function syntax is wrong. You need to have parentheses after the function name. Either of these are valid syntaxes for
main
:int main() {
}
int main(int argc, const char* argv[]) {
}
main
like so:int main() {
int local_variable = 0;
}
or assign to a global variable like so:int global_variable;
int main() {
global_variable = 0;
}
How to make a local variable into a global in JavaScript
You can do the following to access a global inside of a function.
Any variable created outside the scope of a function can be referenced inside of a function.
Var myGlobalVar;
Function myFunction(){
if(....) {
myGlobalVar = 1;
}
}
Python: Change: How do I make local variable global?
Use the global
keyword
# Hello World program in Python
def foo():
global bar #Define the global variable bar
bar = "Hello world" #Set bar to hello world
foo() #Set bar
print bar #Hello world
#Second example
fo = "Initial value" #Initial value
def foobar():
global fo
fo = "Value changed"
bar = "Value changed" #Global variable 'bar' isn't changed because we didn't declare bar global in this function. There is only a local variable named 'bar' created.
print fo #Initial value
print bar #Hello world
foobar() #Change value
print fo #Value changed
print bar #Hello world
http://tpcg.io/yUvZD8 (Try it out)Never make a variable global, just declare it global at the beginning of the function and change the value.
global variable inside main function python
A variable created inside a method (e.g., main) is local by definition. However, you can create a global variable outside the method, and access and change its value from side any other method.
To change its value use the global
keyword.
How do I change the value of a global variable inside of a function
Just reference the variable inside the function; no magic, just use it's name. If it's been created globally, then you'll be updating the global variable.
You can override this behaviour by declaring it locally using var
, but if you don't use var
, then a variable name used in a function will be global if that variable has been declared globally.
That's why it's considered best practice to always declare your variables explicitly with var
. Because if you forget it, you can start messing with globals by accident. It's an easy mistake to make. But in your case, this turn around and becomes an easy answer to your question.
Python function global variables?
If you want to simply access a global variable you just use its name. However to change its value you need to use the global
keyword.
E.g.
global someVar
someVar = 55
This would change the value of the global variable to 55. Otherwise it would just assign 55 to a local variable.The order of function definition listings doesn't matter (assuming they don't refer to each other in some way), the order they are called does.
Related Topics
Print to the Same Line and Not a New Line
How to Handle Exceptions in a List Comprehensions
Pandas - Filter Dataframe by Another Dataframe by Row Elements
How to Read Class Attributes in the Same Order as Declared
How to Fix Selenium Webdriverexception: the Browser Appears to Have Exited Before We Could Connect
Syntaxerror: Unexpected Eof While Parsing
Using Only the Db Part of Django
Pandas Latitude-Longitude to Distance Between Successive Rows
How to Customize Title Bar and Window of Desktop Application
Python, Default Keyword Arguments After Variable Length Positional Arguments
String with 'F' Prefix in Python-3.6
In-Place Type Conversion of a Numpy Array
Python - Requests.Exceptions.Sslerror - Dh Key Too Small
Pandas Number Rows Within Group in Increasing Order
Most Efficient Way to Sort an Array into Bins Specified by an Index Array