Explain Python’s parameter-passing mechanism.

Question

How is it done?

in progress 0
Dhruv2301 55 years 1 Answer 880 views Great Grand Master 0

Answer ( 1 )

  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]

Leave an answer

Browse
Browse