[Flask] Shouldn't is and == operator work same way while doing string comparisons?

Gergely Polonkai gergely at polonkai.eu
Sat May 4 01:10:46 EDT 2019


…in fact, i can easily give OP  a counterexample with strings:

>>> a = 'hello' + ' world'
>>> b = 'hello world'
>>> a
'hello world'
>>> b
'hello world'
>>> a == b
True
>>> a is b
False



Gergely Polonkai
[image: https://]about.me/gergely.polonkai
<https://about.me/gergely.polonkai?promo=email_sig&utm_source=email_sig&utm_medium=email_sig&utm_campaign=external_links>


lau., 4. maí 2019 kl. 01:30 skrifaði Cameron Simpson <cs at cskk.id.au>:

> On 04May2019 00:41, Arup Rakshit <ar at zeit.io> wrote:
> >I tested the both while in REPL.
>
> Testing only tells you what happens in your test, with a aprticular
> environment. It doesn't inform you about the language specification,
> except in so far as a failed test might tell you that your assumption
> was incorrect.
>
> >They work same, why it is then different then flask app code?
>
> Python interning small strings is an implementation detail, not part of
> the language specification. So there is no explicit guarantee that 2
> strings with the same value are the same string object, even for small
> strings.
>
> If you want to act on the value of something, use ==. Using "is" is NOT
> FOR THAT. It is to check if 2 object references refer to the same
> object. This is rarely what you want; it is most useful with singletons
> such as sentinels.
>
> Cheers,
> Cameron Simpson <cs at cskk.id.au>
> _______________________________________________
> Flask mailing list
> Flask at python.org
> https://mail.python.org/mailman/listinfo/flask
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/flask/attachments/20190504/1953da30/attachment-0001.html>


More information about the Flask mailing list