how to put reference in method argument not value python -


hi have code this

a = 7  def add(number):          number+=1          print(number)  add(a)  print(a) 

and prints

8 7 

as know must change because function takes reference everytime in python whats problem , how can solve it?

add not change value of a. number in add new copy object a. can see here different objects, use id(object) returns object identifying number a , nuumber:

a = 7  def add(number):     number+=1     print("'number' id => {0}".format(id(number)))     print(number)   print("'a' id => {0}".format(id(a))) add(a)   print("'a' id => {0}".format(id(a))) print(a) 

output, identifiers might different yours:

'a' id => 19295200 'number' id => 19295188 <- different new object different id 8 'a' id => 19295200 7 

so changes apply on number not effect a. python way of doing reassign object new value:

a = 7  def add(number):          number+=1          print(number)          return number  = add(a)  print(a) 

output:

8 8 

Comments

Popular posts from this blog

Unable to remove the www from url on https using .htaccess -