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.
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,
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