简体   繁体   中英

Is there a way to change a variable within a function if the variable is being passed as an argument?

If I had the code:

num = 3

def add(variable, number):
  variable += number

add(num, 2)
print(num)

It would output 3 because inside the function, the temporary variable variable inside the function is being added to instead of the num variable. Is there a way to make it so when I pass num as the first argument, 3 would be added to num instead of variable and num would be changed to 5?

When you call a function with arguments, you pass the arguments value to the function. That function has no connection to the original variable and it cannot be reassigned to a new value.

The correct way to accomplish your goal here is to return a new value:

num = 3

def add(variable, number):
  return variable + number

num = add(num, 2)
print(num)

You could write it like this:

num = 3


def add(n1, n2):
    return n1 + n2


print(add(num, 2))

The original assignment is not going to update when printing num .

I think you need to explicitly assign the new value to the num variable. For example, you could do:

num = 3

def add(variable, number)
    return variable + number

num = add(num, 2)
print(num) #Prints out 5

Your question touches on one of the more esotheric and easy to misunderstand features of Python: mutables and immutables.

Here is the official documentation on mutable and immutable sequence types;

https://docs.python.org/3/library/stdtypes.html#mutable-sequence-types https://docs.python.org/3/library/stdtypes.html#immutable-sequence-types

As you can tell, only sequences and complex objects are mutable, and thus may be altered by the function.

Base types like integers and floats cannot be changes within the function as you describe, though strings can, as they count as sequences.

In your example, you're using an integer - so this will not work. However, with a string, array, dictionary or complex object (a class of your own design), this _will work.

If you're coming at it from another programming language like C++, think of mutables like pointers, and immutables like references. If you pass a reference, any changes to the object referenced within the scope of the function do not live past the life of the function scope. Any changes to objects with pointers on them, however, do persist.

This post does an excellent job of explaining this in much more depth, and I would highly recommend skimming it;

https://medium.com/@meghamohan/mutable-and-immutable-side-of-python-c2145cf72747

The purpose of the add function is to add two numbers together which will result in a number. This is something completely different than doing operations on already existing variables. If you want to use variables both inside and outside function, you can use the keyword 'global'. This lets the function know that this is a globally defined variable and you will be able to do operations on it. So you can for example do:

num = 3

def add(number):
    global num
    num += number

add(2)
print(num)

Which will print 5

If you just want a function to add two numbers and assign this result to the already globally defined variable, you can use:

num = 3

def add(variable, number):
  return variable + number

num = add(num, 2)
print(num)

This will also print 5

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM