Q:

How are arguments passed by value or by reference in Python?

0

How are arguments passed by value or by reference in Python?

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Answer:

Python uses a mechanism, which is known as “Call-by-Object”, sometimes also called “Call by Object Reference” or “Call by Sharing”.

If you pass immutable arguments like integers, strings, or tuples to a function, the passing acts like Call-by-value. It behaves differently if we pass mutable arguments.

Let us see the code,

student={'Amlendra':28,'Pooja':25,'Amitosh':25}
def test(student):
   new={'Apoorv':5}
   student.update(new)
   print("Inside the function",student)
   return
test(student)
print("outside the function:",student)

Output:

Inside the function {‘Amlendra’: 28, ‘Amitosh’: 25, ‘Apoorv’: 5, ‘Pooja’: 25}
outside the function: {‘Amlendra’: 28, ‘Amitosh’: 25, ‘Apoorv’: 5, ‘Pooja’: 25}

Note: One important thing to note is, in Python every variable name is a reference. When we pass a variable to a function, a new reference to the object is created.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python Interview Questions and Answers

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
What is the output of the below python code?... >>
<< What Is “Call By Reference” In Python?...