Explain Python’s parameter-passing mechanism.
Question
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
It will take less than 1 minute to register for lifetime. Bonus Tip - We don't send OTP to your email id Make Sure to use your own email id for free books and giveaways
Answer ( 1 )
Python utilizes a system, which is known as “Call by Object Reference” or “Call by assignment”.
In the event that you pass arguments like whole numbers, strings or tuples to a function, the
passing is like call-by-value because you can not change the value of the immutable objects
being passed to the function. Whereas passing mutable objects can be considered as call by
reference because when their values are changed inside the function, then it will also be
reflected outside the function.
# Python code to demonstrate
# call by value
string = “TheMonk”
def test(string):
string = “TheDataMonk”
print(“Inside Function:”, string)
test(string)
print(“Outside Function:”, string)
Inside Function: TheDataMonk
Outside Function: TheMonk
# Python code to demonstrate
# call by reference
def add_more(list):
list.append(50)
print(“Inside Function”, list)
mylist = [10,20,30,40]
add_more(mylist)
print(“Outside Function:”, mylist)
OUTPUT:
Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]