Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assignment of objects and fundamental types [duplicate]

Tags:

python

There is this code:

# assignment behaviour for integer
a = b = 0
print a, b # prints 0 0
a = 4
print a, b # prints 4 0 - different!

# assignment behaviour for class object
class Klasa:
    def __init__(self, num):
        self.num = num

a = Klasa(2)
b = a
print a.num, b.num # prints 2 2
a.num = 3
print a.num, b.num # prints 3 3 - the same!

Questions:

  1. Why assignment operator works differently for fundamental type and class object (for fundamental types it copies by value, for class object it copies by reference)?
  2. How to copy class objects only by value?
  3. How to make references for fundamental types like in C++ int& b = a?
like image 325
scdmb Avatar asked Dec 11 '11 12:12

scdmb


People also ask

How do you assign an object to a variable?

You assign an object to such a variable by placing the object after the equal sign ( = ) in an assignment statement or initialization clause.

What assignment operator does the assignment statement use?

The simple assignment operator ( = ) is used to assign a value to a variable. The assignment operation evaluates to the assigned value. Chaining the assignment operator is possible in order to assign a single value to multiple variables.

How do you assign an object to a variable in Java?

First, define a class with any name 'SampleClass' and define a constructor method. The constructor will always have the same name as the class name and it does not have a return type. Constructors are used to instantiating variables of the class. Now, using the constructors we can assign values.

What are assignment operators in programming?

An assignment operator is the operator used to assign a new value to a variable, property, event or indexer element in C# programming language. Assignment operators can also be used for logical operations such as bitwise logical operations or operations on integral operands and Boolean operands.


2 Answers

This is a stumbling block for many Python users. The object reference semantics are different from what C programmers are used to.

Let's take the first case. When you say a = b = 0, a new int object is created with value 0 and two references to it are created (one is a and another is b). These two variables point to the same object (the integer which we created). Now, we run a = 4. A new int object of value 4 is created and a is made to point to that. This means, that the number of references to 4 is one and the number of references to 0 has been reduced by one.

Compare this with a = 4 in C where the area of memory which a "points" to is written to. a = b = 4 in C means that 4 is written to two pieces of memory - one for a and another for b.

Now the second case, a = Klass(2) creates an object of type Klass, increments its reference count by one and makes a point to it. b = a simply takes what a points to , makes b point to the same thing and increments the reference count of the thing by one. It's the same as what would happen if you did a = b = Klass(2). Trying to print a.num and b.num are the same since you're dereferencing the same object and printing an attribute value. You can use the id builtin function to see that the object is the same (id(a) and id(b) will return the same identifier). Now, you change the object by assigning a value to one of it's attributes. Since a and b point to the same object, you'd expect the change in value to be visible when the object is accessed via a or b. And that's exactly how it is.

Now, for the answers to your questions.

  1. The assignment operator doesn't work differently for these two. All it does is add a reference to the RValue and makes the LValue point to it. It's always "by reference" (although this term makes more sense in the context of parameter passing than simple assignments).
  2. If you want copies of objects, use the copy module.
  3. As I said in point 1, when you do an assignment, you always shift references. Copying is never done unless you ask for it.
like image 93
Noufal Ibrahim Avatar answered Oct 24 '22 18:10

Noufal Ibrahim


Quoting from Data Model

Objects are Python’s abstraction for data. All data in a Python program is represented by objects or by relations between objects. (In a sense, and in conformance to Von Neumann’s model of a “stored program computer,” code is also represented by objects.)

From Python's point of view, Fundamental data type is fundamentally different from C/C++. It is used to map C/C++ data types to Python. And so let's leave it from the discussion for the time being and consider the fact that all data are object and are manifestation of some class. Every object has an ID (somewhat like address), Value, and a Type.

All objects are copied by reference. For ex

>>> x=20
>>> y=x
>>> id(x)==id(y)
True
>>>

The only way to have a new instance is by creating one.

>>> x=3
>>> id(x)==id(y)
False
>>> x==y
False

This may sound complicated at first instance but to simplify a bit, Python made some types immutable. For example you can't change a string. You have to slice it and create a new string object.

Often copying by reference gives unexpected results for ex.

x=[[0]*8]*8 may give you a feeling that it creates a two dimensional list of 0s. But in fact it creates a list of the reference of the same list object [0]s. So doing x[1][1] would end up changing all the duplicate instance at the same time.

The Copy module provides a method called deepcopy to create a new instance of the object rather than a shallow instance. This is beneficial when you intend to have two distinct object and manipulate it separately just as you intended in your second example.

To extend your example

>>> class Klasa:
    def __init__(self, num):
         self.num = num


>>> a = Klasa(2)
>>> b = copy.deepcopy(a)
>>> print a.num, b.num # prints 2 2
2 2  
>>> a.num = 3
>>> print a.num, b.num # prints 3 3 - different!
3 2
like image 34
Abhijit Avatar answered Oct 24 '22 18:10

Abhijit