changing global variables within a function in python [duplicate] - python

This question already has answers here:
Using global variables in a function
(25 answers)
Closed last month.
I'm new to python.
I don't quite understand how I need to set variables and change them within a function to be used late. My script needs to get an x and y value from a function which are determined by the size of the chart the function creates. These variable need to be passed to a print command later in the script to output html. So let's say I have global variables:
originx_pct = 0.125
originy_pct = 0.11
But these will need to change when the funtion is run...
def makeplot(temp, entropy,preq):
originx_pct = origin.get_points()[0][0]
originy_pct = origin.get_points()[0][1]
Then printed within the javascript of the html page that is written later...
print('var originx_pct = {};'.format(originx_pct))
print('var originy_pct = {};'.format(originy_pct))
The 2 variables don't change and I just don't understand what I need to do to update them and be able to print them (outside the function). I'm assuming that the function does not know about the variables so it can't change them. If I feed the function the 2 variables as arguments, how do i get the values back out for the print part of the script?

You need to use the global keyword in your function.
originx_pct = 0.125
originy_pct = 0.11
def makeplot(temp, entropy,preq):
global originx_pct, originy_pct
originx_pct = origin.get_points()[0][0]
originy_pct = origin.get_points()[0][1]
You can read more about global here.

In your function, you need to return the values. Change your makeplot to the following:
def makeplot(temp, entropy, preq):
local_originx_pct = origin.get_points()[0][0]
local_originy_pct = origin.get_points()[0][1] # the local_ in the names doesn't mean anything, it is just for clarity.
return local_originx_pct, local_originy_pct
Then, when you call the function, set your variables to its return value.
originx_pct, originy_pct = makeplot(args_and_stuff)
This is considered better practice then directly changing global variables as in ltd9938's answer. It helps to prevent accidentally messing stuff up for other functions. More reasons not to use global

You can either declare the global variables in the function with the lines global originx_pct and global originy_pct, or you can return them when you run the function. To do that, you can do
def makeplot(temp, entropy,preq):
return (origin.get_points()[0][0],origin.get_points()[0][1])
Or
def makeplot(temp, entropy,preq):
return origin.get_points()[0][0:2]
If origin.get_points()[0] has only two elements, you do just this:
def makeplot(temp, entropy,preq):
return origin.get_points()[0]
Then, in your main function, put
originx_pct, originy_pct = makeplot(temp, entropy,preq)
Although I'm not clear on why you're passing temp, entropy, and preq to makeplot, since you don't seem to be using them.

Related

Update Global Variables Input as Parameters Rather Than Returning Results From Function

Goal
I am trying to write a function where one or more of the input parameters is a global variable that is updated by the function, without having to return values from within the function. I am aware I could just return a tuple or two separate values from the function, but I think updating the global variables from within the function would be another interesting method if it is possible.
Reason to do this
Updating global variables with a function is easy when the global variable is known (ie. defined previously in the python script). However, I want to define the function in a separate .py file to easily use the function within other python scripts. Therefore, I need to be able to support different variable names to update.
While this is not at all necessary, I am just interested if this is even possible.
Example Pseudocode
I'm thinking something like this:
def math_function(input_val, squared_result, cubed_result):
squared_result = input_val**2 #update the var input as the squared_result parameter
cubed_result = input_val**3 #update the var input as the cubed_result parameter
where you would input a number for input_val and then global variables for squared_result and cubed_result that the function updates with the result. It would then theoretically work like:
#Declare global variables
b = 0
c = 0
#then somewhere in the code, call the function
math_function(2, b, c)
#check the new values
print(b) #Output: b = 4
print(c) #Output: c = 8
This would allow me to use the function in different python scripts without having to worry about what order the results are returned in.
First: I am in no way advocating this.
You could use the globals builtin function to access a global variable by name:
def gtest(name,value):
globals()[name] = value
gtest('new_global','new_value')
print(new_global)
I am not advocating this too, I would like to hear everyone's thoughts, but this can be done -
my_global = [0, 0]
def my_math_func(x, g):
g[0] = x ** 2
g[1] = x ** 3
my_math_func(3, my_global)

Print a list from another function [duplicate]

This question already has an answer here:
Python - Passing variables through functions
(1 answer)
Closed 1 year ago.
How can I get this to work
Should I use a gloabl variable or a args?
I'm very confused.
def getvenueurl():
# code as part of a loop
venueURList.append(tMD)
# end loop
def getraceurl():
print(venueURList)
getvenueurl()
getraceurl()
In this case you can define the variable outside of functions scope.
Exemple:
venueURList = []
def getvenueurl():
# code as part of a loop
venueURList.append(tMD)
# end loop
def getraceurl():
print(venueURList)
getvenueurl()
getraceurl()
Just return the list in a function. Store the returned value in a variable and pass it through a new function.
def getvenueurl():
# code as part of a loop
venueURList.append(tMD)
return venueURList
# end loop
def getraceurl(lst):
print(lst)
venueList = getvenueurl()
getraceurl(venueList)
Or another option would be to use global venueURList
In order to access a global variable inside a function, you must type:
global venueURList
before it is referenced or modified (in your case, append and print)
Generally, it is a better idea to pass the global variable into the parameter and return it instead.
def getvenueurl(venueList):
venueList.append(tMD)
return venueList

How to change variables in Python functions without using 'global'

I have defined some global parameters in Python (that should also be accesible from other files) and I would like to change them inside the class in which they are defined. I read quite often that one way of doing this is to use 'global' when defining the variables inside the function. However, I also read quite often that 'global' should be avoided basically as it is not good style. Now my question is what other opportunities do I have to change my variables? When just passing them to the function as arguments (this was also one suggestion) their values remain the same.
So here is my code:
from random import random
numberOfBuildings = 10
numberOfVehicles = 10
useMonteCarloMethodForScenarioCreation = True
def monteCarloScenarioGeneration(numberOfBuildings, numberOfVehicles):
numberOfBuildings_MonteCarlo = int( numberOfBuildings *random() *2 + 1)
numberOfVehicles_MonteCarlo = int(numberOfVehicles *random() *2 + 1)
if useMonteCarloMethodForScenarioCreation ==True:
numberOfBuildings = numberOfBuildings_MonteCarlo
numberOfVehicles = numberOfVehicles_MonteCarlo
monteCarloScenarioGeneration(numberOfBuildings, numberOfVehicles)
print('numberOfBuildings: ', numberOfBuildings)
print('numberOfVehicles: ', numberOfVehicles)
The value of the global variables numberOfBuildings and numberOfVehicles are not changed when the function monteCarloScenarioGeneration is called. They keep the same values that they have after being initialized (10). How can I change the global variables without using the keyword global? I'd appreciate any comments.
You should just be able to use return to change the variables outside of the function, if I'm reading this right.
As the last line of the function, put
return numberOfBuildings, numberOfVehicles
and then when calling the function
numberOfBuildings, numberOfVehicles = monteCarloScenarioGeneration(numberOfBuildings, numberOfVehicles)

Python Function Not Changing Variable [duplicate]

This question already has answers here:
Using global variables in a function
(25 answers)
Closed 2 years ago.
I am a c++ programmer and am learning pygame so I am very new to python. I noticed this behavior of functions and don't know what to do about it:
let's say we have a global variable:
x = 10
Now let's say we have a function that changes the value of x:
def foo():
x = 100
Now, inside my main pygame loop, if I call foo(), the variable x is still 10 after the function call. How is this happening???
Inside the function local scope value of x is changed. The x defined outside the function has global scope. To change global value try global var_name statement inside function before updating value
def foo():
global x
x = 100
Inside your function, you implicitly create another variable x, and then set it to 100. To change the global variable try this:
def foo():
global x
x = 100

Same variable names but with different values within two seperate "def" functions. Can they not change down the code somehow?

Short example:
def Tk1():
x = 1
def Tk2():
x = 2
I want "x" to not change down the code into the subsequent variable. Can I have both "x" assigned to different values and not mess up the code within the def?
Yes a variable that is defined in the function, will always stay the same in that specific function, however if you define it outside the two functions the value can change depending on which function you call first:
a = 0
def f_a():
a = 10
return a
def f_b():
a = 4
return a
print(f_a())
print(f_b())
Will return this result:
10
4
The way you showed in your question defines the variable inside the function meaning it is local to that specific function. Note the fact it isn't a global variable.

Categories