0

What is the difference between these expressions?

>>> n = 2
>>> 'a'*2 is 'a'*n
False
>>> 'a'*2 is 'a'*2
True
Artur B
  • 19
  • 4
  • 2
    Read `help('is')`: The operators "is" and "is not" test for an object’s identity: "x is y" is true if and only if *x* and *y* are the same object. An Object’s identity is determined using the "id()" function. "x is not y" yields the inverse truth value. – Corralien Sep 27 '21 at 12:43
  • 1
    The above is slightly different in that you did not explicitly use `==` but it's the same point of confusion. It has nothing to do with string multiplication in this case. The second case might work for a number of reasons (e.g. constant folding optimization) – Iguananaut Sep 27 '21 at 12:44
  • @Iguananaut, yes, python optimization mechanism, I think this is what I need. Thanks – Artur B Sep 27 '21 at 13:03

0 Answers0