Python - Change a function parameter even if it's global - python

I've made a function that I want to use to change a variable, this variable also happens to be global.
def CheckMarkFunc(var):
if var == True:
var= False
elif var == False:
var= True
If var is a global, it wont change. Is there a way to change the var without having to hardcode the global parameter (sound_mute, in this case) into the function itself?
The code below works, but I'd rather not have multiple if statements for each global variable that I want to change, if at all possible:
def CheckMarkFunc(var,button_id,uncheck_texture,checked_texture):
global sound_mute
if var == True:
TextureSwap(uncheck_texture,button_id)
sound_mute = False
if var == False:
sound_mute = True
TextureSwap(checked_texture,button_id)
In both of these cases, the var parameter is the sound_mute boolean.

One option is as follows:
def CheckMarkFunc(var, button_id, uncheck_texture, checked_texture):
if var:
TextureSwap(uncheck_texture, button_id)
else:
TextureSwap(checked_texture, button_id)
return not var
sound_mute = CheckMarkFunc(sound_mute, button_id, uncheck_texture, checked_texture)

Related

How do I set a global variable via function call?

I am trying to change the value of global variable edl_loading to True in function edl_flashing ,somehow it doesn't work?can anyone help understand why does print edl_loading prints False after call to edl_flashing in which I change the value to True
def edl_flashing():
edl_loading = True
print edl_loading
def main ():
global edl_loading
edl_loading = False
print edl_loading
edl_flashing()
print edl_loading #Why this prints as False
if __name__ == '__main__':
main()
OUTPUT:-
False
True
False
You need to use the global in both of your functions - main and edl_flashing
def edl_flashing():
global edl_loading
edl_loading = True
print edl_loading
Without the global declaration in the function, the variable name is local to the function.
The above change prints out
False
True
True

Pass variables through functions in Python 3

Is it possible to pass global variables through a function. For example
def start():
global var
if var == 0:
print("Error")
else:
while var> -1:
print(var)
var = var - 1
Your start function explicitly allows access to a global variable named var. As evidenced by your error, you have no such variable defined. Please initialize the variable before the function:
var = 25
def start():
global var
# the rest of your function
# goes here after global var

Why won't a global variable change when assigning to it in a function?

I'm making a game in python, and I have some code set up as such:
istouching = False
death = True
def checkdead():
if istouching:
print "Is touching"
death = True
while death is False:
print death
# game logic
I know the game logic is working, because "Is touching" prints, but then when I print out the value of death, it remains false.
use global to change global variables inside a function, otherwise death=True inside checkdead() will actually define a new local variable.
def checkdead():
global death
if istouching == True: #use == here for comparison
print "Is touching"
death = True
Make checkdead return a value:
def checkdead():
if istouching:
print "Is touching"
return True
death = checkdead()
You could also use global, as #AshwiniChaudhar shows, but I think it is preferable to write functions that return values instead of functions that modify globals, since such functions can be unit-tested more easily, and it makes explicit what external variables are changed.
PS. if istouching = True should have resulted in a SyntaxError since you can not make a variable assignment inside a conditional expression.
Instead, use
if istouching:
That's scope-related.
death = False
def f():
death = True # Here python doesn't now death, so it creates a new, different variable
f()
print(death) # False
death = False
def f():
global death
death = True
f()
print(death) # True

How to set the variable inside a function scope?

Could I set a variable inside a function scope, knowing that I sent this variable like a parameter..
See the example:
def test(param):
param = 3
var = 5
test(var)
print var
I want the value printed be 3, but it doesn't happen.
How can I do that?
Thanks..
You can return the value of param like this:
def test(param)
param = 3
return param
var = 5
var = test(var)
Or you can use a global variable.
Better to use return than a global:
def test(param):
param = 3
return param
var = 5
var = test(var)
print var
The global statement allows you to assign to variables declared outside a function's scope.
var = 5
def test():
global var
var = 3
test()
print var
However, I have found that I seldom need to use this technique. The functional programming model makes this less important.

Trouble with variable. [Python]

I have this variable on the beginning of the code:
enterActive = False
and then, in the end of it, I have this part:
def onKeyboardEvent(event):
if event.KeyID == 113: # F2
doLogin()
enterActive = True
if event.KeyID == 13: # ENTER
if enterActive == True:
m_lclick()
return True
hookManager.KeyDown = onKeyboardEvent
hookManager.HookKeyboard()
pythoncom.PumpMessages()
and I get this error when I press enter first, and when I press F2 first:
UnboundLocalError: local variable 'enterActive' referenced before assignment
I know why this happens, but I don't know how can I solve it...
anyone?
See Global variables in Python. Inside onKeyboardEvent, enterActive currently refers to a local variable, not the (global) variable you have defined outside the function. You need to put
global enterActive
at the beginning of the function to make enterActive refer to the global variable.
Approach 1: Use a local variable.
def onKeyboardEvent(event):
enterActive = false
...
Approach 2: Explicitly declare that you are using the global variable enterActive.
def onKeyboardEvent(event):
global enterActive
...
Because you have the line enterActive = True within the functiononKeyboardEvent, any reference to enterActive within the function uses the local variable by default, not the global one. In your case, the local variable is not defined at the time of its use, hence the error.
enterActive = False
def onKeyboardEvent(event):
global enterActive
...
Maybe you are trying to declare enterActive in another function and you aren't using the global statement to make it global. Anywhere in a function where you declare the variable, add:
global enterActive
That will declare it as global inside the functions.
Maybe this is the answer:
Using global variables in a function other than the one that created them
You are writing to a global variable and have to state that you know
what you're doing by adding a "global enterActive" in the beginning of
your function:
def onKeyboardEvent(event):
global enterActive
if event.KeyID == 113: # F2
doLogin()
enterActive = True
if event.KeyID == 13: # ENTER
if enterActive == True:
m_lclick()
return True

Categories