Python

Pass by Reference vs. Value in Python

After knowing Python, you can find instances where the functions do not alter the arguments in a spot as you would anticipate, particularly if you are acquainted with many other computer languages. Many languages use method arguments as references, defined as passing by Reference to current variables. If you’re an advanced Python developer who wants to grasp Python’s particular way of treating method arguments, then this guide is really for you.

Python Supports Pass by Object Reference

Pass-by-reference and pass-by-value are undeniably the two most well recognized and easily understandable ways to parameter passing between programming languages. Python is, sadly, ‘pass-by-object-reference,’ neither “Pass by Value” nor “Pass by Reference,” often termed as “Call by Object Reference” as well as “Call by Sharing.” It’s useful to look more closely at the concept itself by splitting it down into segments while you plunge into the technical specifics of passing by Reference:

Pass: this means supplying a method with an argument.

By Reference: this means the argument you move over to the method refers to a variable that now resides in storage instead of a different replica of that variable.

As you assign the method a reference to a defined variable, the variable it corresponds to would be explicitly influenced by all operations on this Reference. Now let us consider an example of how, in practice, this works. In this example, we have defined a variable ‘arg’ having a value of 4. Throughout this scenario, the variable ‘arg’ wasn’t modified in its place. Python does seem to handle your provided argument instead of a reference to a present variable as a self-contained value.

Would this mean that instead of by Reference, Python moves arguments by value? Python moves arguments through assignment, so neither by Reference nor with value. The logic for this is two-fold:

Currently, the parameter passing in is a pointer to an object. Certain types of data are mutable, and some are not mutable.

If we move a mutable object into a function, the function acquires a reference to the very same object so you can mutate it all to the satisfaction of your soul; however, the external scope would know nothing before you rejoin the Reference in the function. Once you’re finished, the external Reference will only aim at the actual object. If you move an immutable object to a function, the external Reference can always not be re-bonded, and you can’t just mutate the object. To make things much more straightforward, let’s understand one by one.

Pass By Reference

First of all, you have to understand that the variable’ mylist’ itself is not a list but refers to a list having values. You can call the variable ‘mylist’ a container having values within it. The list values are objects. The ‘mylist’ variable has been straight delivered into the function with its contents.

Both “list” & “my list” seem to be the same storage variable in the code example below and thus apply to the very same storage object. That’s why on output, it prints ‘Saeed.’

Any action done on the variable or entity would be immediately mirrored to the caller method. The method may entirely modify the value of the variable and aim it at a wholly distinct object. As you can see in the function ‘set_list,’ we have changed the list contents and printed a whole new list having the element ‘Aqsa.’ This is because we have returned the modified list and printed it at the same line as the caller.

The method may also reallocate the variable’s elements for the same result as below. You can see we have appended a new value to the list, and the change has been reflected. We have appended a unique string to a list and returned it to the caller. To conclude, the method and the caller have been using the same variable and object throughout the pass-by relation.

Pass by Value

Through pass by value, the method is supplied with a duplicate of the argument object that the caller assigns to it. This ensures that the original item remains unchanged and all modifications made are retained at separate memory locations in a replica of the same object.

It is equally valid with any operations executed on a variable or the entity by the method. Duplicates of variables & objects in the caller method’s scope are entirely separated to summarize them.

Pass Object by Reference

Throughout this situation, since Python is distinct, Python’s methods obtain the very similar object reference in the storage as the caller refers to. Conversely, the technique doesn’t obtain the variable ‘mylist’ (the container). The caller method stores the same object; the method generates one’s container and generates an altogether fresh index for its own, just as in pass-by-value.

The caller and the method speak of the same object in the storage, but when the appended method applies an external item to a list, the caller entity is modified. They have multiple labels, but they’re the same things. Both variables hold a very similar object. That’s the sense behindhand its relation to moving by the object. In the storage, the method and caller use a similar object yet catch them through multiple variables. The caller variable (the container) will not be changed by any modifications made to the method variable (the container); only the data or the content is modified.

Conclusion

Python operates independently from languages that accept the moving by Reference or value of arguments. Method arguments are local variables that have been allocated to each value transferred to the method. But that still doesn’t preclude you from obtaining the same outcomes you would find in other languages while moving arguments by proxy.

About the author

Aqsa Yasin

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.