sureshdsk.dev

sureshdsk.dev

Is Python use Call by value or Call by reference?

Is Python use Call by value or Call by reference?

learn how parameters are passed in python and mutable and immutable objects in python

Subscribe to my newsletter and never miss my upcoming articles

In C programming, Values to a function can be passed by its value or by its reference. These methods are known as Call by value and Call by reference respectively.

Let's understand what are mutable and immutable objects.

Mutable Objects

list, dict and set are mutable objects which means you can manipulate its value after definition. User defined classes and objects are also mostly mutable. Let's play with list data type.


languages = ["Python", "Golang", "Javascript"]
languages[0] = "CPython"
languages.append("Rust")
print(languages)

As you can see, we can manipulate items inside languages after it is defined.


languages = ["Python", "Golang", "Javascript"]
print('At top level scope: ', languages)


def my_print(skills):
    skills[0] = "CPython"
    skills.append("Rust")
    print('Inside my_print: ', languages)


my_print(languages)
print('At top level scope: ', languages)

In this example above, we can see that adding new item and modifying an existing item inside my_print makes change in original object languages. This is because list is a mutable object, so original object is modified.

check for identity of the object

languages = ["Python", "Golang", "Javascript"]
print('At top level scope: ', languages)
print('Id of languages: ', id(languages))

def my_print(skills):
    print('Id of skills before manipulation: ', id(skills))
    skills[0] = "CPython"
    skills.append("Rust")
    print('Inside my_print after manipulation: ', skills)


my_print(languages)
print('At top level scope: ', languages)

As you can see both languages and skills point to same object. Lets look at how immutable objects behave,

Immutable Objects

language = "python"
print('At top level scope: ', language)


def my_print(skill):
    skill = skill.capitalize()
    print('Inside my_print after manipulation: ', skill)


my_print(language)
print('At top level scope: ', language)

We can notice that making changes to skill inside my_print doesn't affect the var language since it is an immutable object. So only value is passed.

Conclusion

In Python, everything is an object. Values are passed as object's reference. Based on whether the object is Mutable or Immutable, decision is made to call by its value or by its reference. Immutable object types are passed as values. Mutable object types are passed as references.

If you find this article helpful, please do like and comment on this article. If you have any suggestions or feedbacks, feel free to comment.

Stay tuned for upcoming articles. Subscribe to the newsletter and Connect with me on twitter to get my future articles.

Interested in reading more such articles from Suresh Kumar?

Support the author by donating an amount of your choice.

#python#python3
 
Share this