VALUE AND REFERENCE

Question

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

in progress 0
Pragya Rani 4 years 4 Answers 857 views Contributor 1

Answers ( 4 )

  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]

  2. In python, there is no call by value or call by reference instead it uses “Call by Object Reference” or “Call by assignment”.

    Just to set some background, everything in python is an object. Assignment is binding a name with an object.Each binding has a scope that defines its visibility, usually the block in which the name originates.
    eg: a= “Peter”
    b= “Peter”
    #’a’ and ‘b’ are referring to the same object that is “Peter”
    a=”John”
    # Now ‘a’ refers to “John” instead of “Peter”, both string objects exist in memory but binding of name ‘a’ differs.

    If the object being passed in argument is mutable itself then the changes will be visible outside the scope of the function.
    Example :
    def foo(latest):
    latest.append(42)
    print(latest)
    # >> [42]

    original= []
    foo(original)
    print(original)
    # >> [42]
    On the other hand, if latest refers to an immutable object, the most that foo function can do is create a name latest in its local namespace and bind it to some other object.
    Example :
    def foo(latest):
    latest=”new”
    print(latest)
    # >> new

    original= “old”
    foo(original)
    print(original)
    # >> “old”

  3. In Python, (almost) everything is an object. What we commonly refer to as “variables” in Python are more properly called names. Likewise, “assignment” is really the binding of a name to an object. Each binding has a scope that defines its visibility, usually the block in which the name originates.
    In Python,Values are passed to function by object reference.
    if object is immutable(not modifiable) than the modified value is not available outside the function.
    if object is mutable (modifiable) than modified value is available outside the function.

  4. 1) In call-by-value, the argument expression is evaluated, and the result of this evaluation is bound to the corresponding variable in the function. So, if the expression is a variable, a local copy of its value will be used, i.e. the variable in the caller’s scope will be unchanged when the function returns
    2) In call-by-reference evaluation, a function gets an implicit reference to the argument, rather than a copy of its value. The advantage of call-by-reference consists of the advantage of greater time- and space-efficiency because arguments do not need to be copied. The disadvantage is that variables can be “accidentally” changed in a function call. So special care has to be taken to “protect” the values, which shouldn’t be changed
    3) Python uses a mechanism, which is known as “Call-by-Object”, sometimes also called “Call by Object Reference” or “Call by Sharing”
    4) If you pass immutable arguments like integers, strings or tuples to a function, the passing acts like call-by-value. The object reference is passed to the function parameters. They can’t be changed within the function, because they can’t be changed at all, i.e. they are immutable
    5) If we pass mutable arguments by object reference, they can be changed in place in the function. If we pass a list to a function, we have to consider two cases: Elements of a list can be changed in place, i.e. the list will be changed even in the caller’s scope. If a new list is assigned to the name, the old list will not be affected, i.e. the list in the caller’s scope will remain untouched

Leave an answer

Browse
Browse