Home > Mobile >  How can I change the "a" vaule use the patato function?
How can I change the "a" vaule use the patato function?

Time:06-29

def patato(a):
    a  = 1
def tomato():
    a = 0 
    patato(a)
    print(a)
tomato()

How can I change the "a" vaule use the patato function?

CodePudding user response:

Assigning to the parameter variable doesn't affect the caller's variable, since variables are passed by value, not reference (in the case of mutable objects, the value is a reference to that object, but not a reference to the variable).

patato should return the new value, then you can assign it back to the variable.

def patato(a):
    return a   1

def tomato():
    a = 0
    a = patato(a)
    print(a)

tomato()

CodePudding user response:

Try this and see if you can understand it, or ask further questions:

def patato(a):
    a  = 1
    return a
    
def tomato():
    a = 0 
    return patato(a)
    
print(tomato())   # 1
  • Related