Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

How to pass arguments by reference in Python function?

In the given code

def foo(x):
    x.append("20")
    return x
a = ["10"]
b = foo(a)
print b
print a

Are the arguments passed by reference in the python function?


1 Answer
Malhar Lathkar

In Python, function arguments are always passed by reference. This can be verified by checking id() of factual, formal arguments and returned object

def foo(x):
  print ("id of received argument",id(x))
  x.append("20")
  return x
a = ["10"]
print ("id of argument before calling function",id(a))
b = foo(a)
print ("id of returned object",id(b))
print (b)
print (a)

The id() of a, x inside foo() and b are found to be same.

id of argument before calling function 1475589299912
id of received argument 1475589299912
id of returned object 1475589299912
Advertisements

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.