Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Python know two string variables point to the same object? [duplicate]

I am new to Python and I have to say the way Python treats variable assignment and function arguments is very confusing. Here is something I don't understand. If I define two strings with explicitly the same content like 'abc', then they are actually the same object, as shown below.

x = 'abc'
y = 'abc'
x is y
True

This gets me thinking how Python knows they are the same. By comparing the literals in the code? If there are million different things happening between x = 'abc' and y = 'abc', does Python go back all the way and say there was an object 'abc' already so I am not going to create a new 'abc'?

I wondered what would happen if I do the same but with a really long and complex string. This is what happened.

x = 'nao;uh gahasjhd;fjkhag;sjdgfuiwgfashksghdfaihghehwq3473fsd_@'
y = 'nao;uh gahasjhd;fjkhag;sjdgfuiwgfashksghdfaihghehwq3473fsd_@'
x is y
False
x == y
True

Now you can see why I am confused. So Python only check for "simple" strings but not long strings when creating new string objects? Then how complex/long is too complex/long?

like image 217
Frank Avatar asked Jul 06 '18 17:07

Frank


People also ask

How would you confirm that 2 strings have the same identity Python?

It means, if two variables are assigned the same string value, they are really referring to the same string object in memory. This fact can be verified by checking their id() value. Hence, comparison operator == for checking equality returns True if two string operands have same id() value, and False otherwise.

Can two variables refer to the same object Python?

The is keyword is used to test if two variables refer to the same object. The test returns True if the two objects are the same object. The test returns False if they are not the same object, even if the two objects are 100% equal. Use the == operator to test if two variables are equal.

Can we compare two strings using == in Python?

To compare two strings, we mean that we want to identify whether the two strings are equivalent to each other or not, or perhaps which string should be greater or smaller than the other. This is done using the following operators: == : This checks whether two strings are equal.

How do you check for string equality in Python?

String Equals Check in Python In python programming we can check whether strings are equal or not using the “==” or by using the “. __eq__” function. Example: s1 = 'String' s2 = 'String' s3 = 'string' # case sensitive equals check if s1 == s2: print('s1 and s2 are equal.


1 Answers

There is difference in x is y and x==y
x is y will check if x and y are pointing to same object in heap or not.
While x == y will check if value of x and y is same or not.

Now lets see why you got that two different result

If length of value is small (at-most 3 to 4 digit) only then python check if there is another object present in heap with same value or not.If present then it does not create new object ,and if not present it creates new object.
If length of value is Big(more than 4 digit) python creates new object it does not check if object with same value is already present or not.

When length of string,int,float is small

In python when two variables are having same string,int or float value and if the length of value is small then both the variables point to same object i.e. only one object is creted in heap memory.
let's try on your own by taking this example.

a=10
b=10
a is b
True     #output
a == b
True     #output  

Here a is b checks if a and b are refering(i.e. pointing) to same object in heap or not.
Since 10 is of length 2 which is small python interpreter will create object value 10 only once and that's why a and b will refer to same object and output is True

And a == b will check if value of a is equal to value of b.
Since value of a is 10 and value of b is also 10 so output is True

You can try with string value as well e.g

s1='abc'
s2='abc'
s1 is s2
True      #output
s1 == s2
True      #output  

When length of string,int,float is big

Now when length of string,int,float is big then python interpreter does not check if there is object with same value in heap, it directly creates new object even if there was object present with same value

x = 'nao;uh gahasjhd;fjkhag;sjdgfuiwgfashksghdfaihghehwq3473fsd_@'
y = 'nao;uh gahasjhd;fjkhag;sjdgfuiwgfashksghdfaihghehwq3473fsd_@'
x is y
False
x == y
True

Here since nao;uh gahasjhd;fjkhag;sjdgfuiwgfashksghdfaihghehwq3473fsd_@ is too long python interpreter will not check if there is another object with same value present in in heap ,it will create an object an x will refer to that object.
Now again nao;uh gahasjhd;fjkhag;sjdgfuiwgfashksghdfaihghehwq3473fsd_@ is assigned to y but since length of string is large python interpreter will create new object(even tough that object is already present)
Now since x and y are refering to different object output will be False
And since x and y are having same value output will be True

You can try this e.g also

a = 10000
b = 10000
a is b
False      #output
a == b
True      #output  

Why Python does this

Python does it to reduce the Interpretation time(i.e. execution time for code) If python keeps checking whether that long string(e.g whose length is 10) are already present in heap or not.It will require more time because it will compare letter by letter with all the objects.Comparing 10 digit will consume lot of time.
While if length of string is less than 4 than it is easy to compare(because only 3 letter are needed to compare) and it will not take much time.

like image 82
swapnil Avatar answered Nov 14 '22 23:11

swapnil