0

This is a simplified section of code from a game I'm trying to make. There is a condition in primary_function that if satisfied will run the action, in this case secondary_function fed into it, action itself being a function which alters a particular variable. Problem is that it doesn't alter the variable. Print(variable) returns 1.

variable = 1

def secondary_function(var):
    var += 1

def function(parameter_1, parameter_2, action = None, var = None):
    if condition == 'satisfied':
        action(var)

function(1, 2, secondary_function, variable)

print(variable)
2
  • You aren't changing the value of variable. Commented Dec 22, 2015 at 10:48
  • @Matthews Mathai I see, what alteration do you suggest? I need the code to be able to be general enough for lots of different variables and lots of different secondary_functions. Commented Dec 22, 2015 at 10:55

2 Answers 2

2

Variables in Python are not passed by reference, and there is no way to do so. So a function that takes an integer argument and just increments it can never impact the original variable. If you want to modify the original variable, you will have to assign it a new value instead.

def secondary_function(var):
    return var + 1

variable = 1
variable = secondary_function(variable) # variable is now 2

Similary, you will have to modify your function function too to return the value instead:

def function (parameter_1, parameter_2, action = None, var = None):
    if parameter_1 == 1:
        return action(var)

variable = function(1, 2, secondary_function, variable)

Python has no notion of pass-by-reference or pass-by-value. Instead, every variable is passed by assignment. That means that whenever you call a function and pass a variable, the behavior is the same as if you just did function_argument = variable; you are assigning the value of the source variable to the function argument. Since Python is object oriented, this effectively means that you copy the reference to the object that variable refers to. As such, when the object is mutable, you can modify it and impact other variables referencing the same object. But for immutable objects (ints, strings, tuples, etc.), you cannot modify the existing object, so when you “modify” it, you always assign a new object to the variable, without affecting all the other variables that still point to the old object.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the answer and explanation, it's going to be hard to fit this into the rest of my code but I see it is the right way about it.
0

Function arguments in Python as passed by value. Thus, function(var) sees a copy of variable in var and secondary_function(var) sees yet another copy.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.