Pranjal Kumar
Pranjal Kumar

Reputation: 147

Difference between Python object reference and variables in cases of mutable and immutable objects

I was reading a book on python:

Once we have some data types, the next thing we need are variables in which to store them. Python doesn’t have variables as such, but instead has object references. When it comes to immutable objects like ints and strs, there is no discernable difference between a variable and an object reference. As for mutable objects, there is a difference, but it rarely matters in practice."

Now it is not mentioned what is the difference and I am unable to clear myself on this concept.

What is object reference (Is it the memory address for cpython)? Actually what is a variable is it a name that is bound to the object reference (my understanding of memory reference is, that it is a address in the memory for cpython, clear me on this if I am wrong).

It will be helpful if it can be explained in the light of it's internal implementation in C and how are these things managed and working internally.

Also it would be helpful if some resources may be provided that explain cpython implementation in detail.

Upvotes: 1

Views: 2970

Answers (2)

Le Ngoc Thuong
Le Ngoc Thuong

Reputation: 309

  • You can refer to this link for more the details:How do I pass a variable by reference or this: https://www.python-course.eu/passing_arguments.php
  • In C++ there are 3 methods passing an argument to a function
  • Pass by value: Normally the value of a variable will be copied and pass to function, so the value of that variable does not change after function changing its value. In python, if you passing an immutable variable (like ints or strs), the value of those variables will not be changing.

Ex: C++

void passByValueFunction(int a)
{
    //Changing argument
    a = 20;
}
int main ()
{
    int a = 10;
    std::cout << a;
    passByValueFunction(a);
    //a will not change after passByValueFunction changes its value
    std::cout << a;  
}

Ex: Python

def passByValue(a):
    a = 20
a = 10
print a
passByValue(a)
# a will not change
print a

+ Pass by address and pass by reference: C++ can pass by address (by pointer) or by reference (reference variable). The result is the value of variable will changes after function changing it. In python, if you pass a mutable variable to a function, its value will change.

Ex: C++

    void passByRef(int &a)
    {
      a = 20
    }
    int main ()
    {
      a = 10;
      std::cout << a;
      passByRef(a);
      //The value of a will be change
      std::cout << a;
    }

Ex: Python

def passByRef(list):
  list = [22,33]
list = [11,44]
print list
passByRef(list)
#List will be change
print list

Upvotes: 4

Martijn Pieters
Martijn Pieters

Reputation: 1121486

The author is saying that you may as well ignore the references when it comes to immutable objects, because you will always replace the reference to an int or a str object with a reference to another such object.

So

a = 5
a = a + 10

will set a to be a reference to a new int object. After all, you can't mutate that object directly.

They further distinguish between variables and object references. Python's names (often called variables) do not store the value directly (as you would in C or other languages), they are always references. a = 5 makes a a reference to an object of type int that has a value of 5, while in C it would require a to be an int variable that holds the value directly.

I would find that explanation confusing too, because what happens with mutable objects is that they themselves contain references that you alter. A list is just a sequence of references, each addressed by an index. You can mutate the list by adding more references (extending the list) or by altering the references (assigning to indices).

I strongly recommend you read Ned Batchelder's Facts and myths about Python names and values, it is a far clearer explanation of how the Python model works.

I'd not worry about how this is implemented in C much. CPython just uses a lot of pointers to struct objects on a memory heap.

Upvotes: 3

Related Questions