0

So this question is not a super concrete question but I can give some examples. I am very afraid of Python since you never know when a variable is pointing at a new object or just have two variables pointing at the same object.

For example I am scared of assigning like this:

def function(self):
    number = self.value
    return number

In this case I don't want number to change if I change self.value.

Also just copying lists like:

list = [1,2,3]
list2 = list

In this case if I change list, list2 will also be changed which is not desirable. Why would I copy it if i don't want it to be a new variable!

Anyway, I just wonder if anybody knows a safe method to create new variables in these cases or maybe has a complete explanation of when a new object is created and when two variables point at the same object. I want to code in peace and not worry about this all the time because my code is big and I can't sit and check which variables have changed and which have not after every calculation.

The question is related to mutability and immutability so the link to the question: Immutable vs Mutable types, might be good but this is another way of asking the question which might be helpful for some. Also this is more focused on avoiding mutating.

Kanerva Peter
  • 237
  • 5
  • 13
  • 2
    Simple rule: assignment **never** creates a new object. Always, after the assignment, the name on the left simply refers to the object on the right. – Robᵩ Dec 21 '17 at 18:27
  • So I must always assign a value to a new variable A, and then i can assign a value from variable B to A, and this will always create a copy of the object? – Kanerva Peter Dec 21 '17 at 18:38
  • Possible duplicate of [Immutable vs Mutable types](https://stackoverflow.com/questions/8056130/immutable-vs-mutable-types) – jwodder Dec 21 '17 at 18:40
  • 1
    @KanervaPeter - No, assigning from B to A will never create a copy of the object. But see the accepted answer and the duplicate question listed above for a more complete explanation. – Robᵩ Dec 21 '17 at 18:58
  • How about initiating a new class object with some initiate parameters. If I just pass the name of the variable to the constructor parameter. Is the object just assigned or does it create a new object? – Kanerva Peter Dec 21 '17 at 19:06

1 Answers1

1

In Python, like in many languages, some objects are said to be mutable and some are said to be immutable. An immutable object is an object that once created cannot change, such as an int.

x = 3
y = x

In the previous example both x and y point to the same object, but since 3 is immutable, it is not a problem.

Some other objects are mutable, such as a list. It means there are ways to change their value.

l1 = [1, 2, 3]
l2 = l1 # This does not copy the object, it simply makes the name l2 point to it

Here the exact same as for the int happens, both variables point to the same object, but there exist a way to change that object. By example by doing l2.append(4).

The best way to avoid what you are talking about here is by adopting good practices. By example, keep your function pure and make it clear when they are not.

A pure function is a function that has no side effects and returns a brand new object. So it does not mutate the arguments then return them, but copy them and return a new one.

In the example you gave, a way to achieve that would be to use deepcopy.

from copy import deepcopy

l1 = [1, 2, 3]
l2 = deepcopy(l1) # Here deepcopy actually created a new object and returned it

I encourage you to read about which objects are mutable and which are not and then adjust your practices based on that new knowledge.

Olivier Melançon
  • 21,584
  • 4
  • 41
  • 73