0

I cannot understand why the a1, b1 variables refer to the same thing, but a2, b2 do not refer to the same thing:

a1="abc123"
b1="abc123"

print(a1 is b1)

Output:

True
a2="+abc123"
b2="+abc123"

print(a2 is b2)

Output:

False
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Pouya
  • 13
  • 2
  • Can you clarify what you mean by "why"? Strings are immutable, the Python language gives no guarantee whatsoever on their identity – they may be identical, they may not be identical. If you are asking for Python in general, there is literally no reason for either result. Are you asking why some specific implementation works like that? How it's even possible? Why ``+``, the length, or something is the trigger? – MisterMiyagi Jan 18 '21 at 12:12
  • Be aware that this behaviour depends *at least* on the implementation and whether the statements are translated together (i.e. the same module or the same REPL input). You would have to clearly specify that. – MisterMiyagi Jan 18 '21 at 12:15

2 Answers2

0
a2="+abc123" 
b2="+abc123"
print(a2 is b2)

it says True You may be leaving some empty space

XyYang
  • 31
  • 4
0

You misunderstood what the is operator tests. It tests if two variables point the same object, not if two variables have the same value.

From the documentation for the is operator:

The operators is and is not test for object identity: x is y is true if and only if x and y are the same object.

I suggest you use the equality operator(==)

Here is how to use the equality operator for your scenario:

a2="+abc123"
b2="+abc123"

print(a2 == b2)

Output:

True

If you use the id() operator witch Return the “identity” of an object. This is an integer which is guaranteed to be unique and constant for every object during its lifetime. Two objects with non-overlapping lifetimes may have the same id() value.

Test for b1's id

id(a2)

Output:

1600416721776

Test for b2's id

id(b2)

Output:

1600416732976

Using the id() operator you can see that they do not point to the same object

It is worth a mention that the it is a bad idea to use the is operator for strings because Alphanumeric string's always share memory and Non-alphanumeric string's share memory if and only if they share the same block(function, object, line, file):

Alphanumeric string's:

a='abc'
b='abc'
print('a is b: ' a is b)

Output:

a is b: True

non-Alphanumeric string's:

A='+abc123'; B='+abc123';
C='+abc123';
print('A is B: 'A is B)
print('A is C: 'A is C)

Output:

A is B: True 

A is C: False 

TERMINATOR
  • 1,180
  • 1
  • 11
  • 24
  • I don't think the OP misunderstood the operator. They explicitly mention "a1,b1 variables refer to *the same thing*", implying identity instead of equality. – MisterMiyagi Jan 18 '21 at 12:22
  • The a1, b1 have the same id in the OP code, and in my tests as well. That a2, b2 in contrast do not is exactly what the OP is asking about. – MisterMiyagi Jan 18 '21 at 12:29
  • Please reread the question. The OP is aware that ``a2`` and ``b2`` are not identical. – MisterMiyagi Jan 18 '21 at 12:43
  • "Alphanumeric string's always share memory and Non-alphanumeric string's share memory" that is _not_ true. Equal strings _may_ share memory, you can't rely on `is` returning `True` _or_ on `is` returning `False`. – Jasmijn Jan 18 '21 at 14:43
  • @Jasmilin please look at this before you incorrectly criticize my answer: https://stackoverflow.com/questions/16756699/is-operator-behaves-differently-when-comparing-strings-with-spaces – TERMINATOR Jan 18 '21 at 16:22