Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does assigning another variable to a string make a copy or increase the reference count

Tags:

python

On p.35 of "Python Essential Reference" by David Beazley, he first states:

For immutable data such as strings, the interpreter aggressively shares objects between different parts of the program.

However, later on the same page, he states

For immutable objects such as numbers and strings, this assignment effectively creates a copy.

But isn't this a contradiction? On one hand he is saying that they are shared, but then he says they are copied.

like image 322
Terrence Brannon Avatar asked Jul 20 '12 19:07

Terrence Brannon


People also ask

Can you assign a string to a variable?

To assign it to a variable, we can use the variable name and “=” operator. Normally single and double quotes are used to assign a string with a single line of character but triple quotes are used to assign a string with multi-lines of character.

Does Python copy strings?

In Python, strings are immutable, meaning that their value cannot change over the course of the program. Being immutable also means that a string cannot directly have a copy. If a new variable is declared and is directly assigned the value of a given string variable, this would not create a copy of the original string.

Which method of string class copies and returns part of an existing string object?

The Copy method returns a String object that has the same value as the original string but represents a different object reference. It differs from an assignment operation, which assigns an existing string reference to an additional object variable.

What is string copy in c#?

Copy(String) Method is used to create a new instance of String with the same value as a specified String. In other words, this method is used to copy the data of one string into a new string. The new string contains same data like an original string but represents a different object reference.


1 Answers

An assignment in python never ever creates a copy (it is technically possible only if the assignment for a class member is redefined for example by using __setattr__, properties or descriptors).

So after

a = foo()
b = a

whatever was returned from foo has not been copied, and instead you have two variables a and b pointing to the same object. No matter if the object is immutable or not.

With immutable objects however it's hard to tell if this is the case (because you cannot mutate the object using one variable and check if the change is visible using the other) so you are free to think that indeed a and b cannot influence each other.

For some immutable objects also Python is free to reuse old objects instead of creating new ones and after

a = x + y
b = x + y

where both x and y are numbers (so the sum is a number and is immutable) may be that both a and b will be pointing to the same object. Note that there is no such a guarantee... it may also be that instead they will be pointing to different objects with the same value.

The important thing to remember is that Python never ever makes a copy unless specifically instructed to using e.g. copy or deepcopy. This is very important with mutable objects to avoid surprises.

One common idiom you can see is for example:

class Polygon:
    def __init__(self, pts):
        self.pts = pts[:]
    ...

In this case self.pts = pts[:] is used instead of self.pts = pts to make a copy of the whole array of points to be sure that the point list will not change unexpectedly if after creating the object changes are applied to the list that was passed to the constructor.

like image 90
6502 Avatar answered Sep 30 '22 12:09

6502