Python Forum
how to silence this warning?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to silence this warning?
#7
(Nov-12-2022, 08:52 PM)Skaperen Wrote: i'd like to know why it is considered a mistake. doing so for int values that could be == to a boolean seems useful to me
Try the following experiment
>>> x = 533433344
>>> y = 533433344
>>> x is y
False
>>> 
>>> x = 1
>>> y = 1
>>> x is y
True
When the value is 533433344, the two variables defined this way are distinct Python objects. When the value is 1, the two variables are the same object. But if your code takes this for granted it is a mistake because it is currently an implementation detail of CPython. Your code may work on your machine but not in another machine using a different implementation of Python.

This is how I understand the issue.

Another experiment
>>> x = 533433344
>>> x is 533433344
<stdin>:1: SyntaxWarning: "is" with a literal. Did you mean "=="?
False
>>> 
Now why is it always a mistake? I think you cannot make assumptions about the Python object that the compiler creates when it processes the literal. So basically you are testing whether an object is the same instance as another object which you know nothing about. Your mistake is to think that you can do anything useful with the boolean result of that test.
Reply


Messages In This Thread
how to silence this warning? - by Skaperen - Nov-11-2022, 05:24 AM
RE: how to silence this warning? - by Gribouillis - Nov-11-2022, 01:10 PM
RE: how to silence this warning? - by Skaperen - Nov-11-2022, 05:39 PM
RE: how to silence this warning? - by snippsat - Nov-11-2022, 06:36 PM
RE: how to silence this warning? - by Gribouillis - Nov-11-2022, 09:04 PM
RE: how to silence this warning? - by Skaperen - Nov-12-2022, 08:52 PM
RE: how to silence this warning? - by Gribouillis - Nov-12-2022, 09:39 PM
RE: how to silence this warning? - by Skaperen - Nov-12-2022, 10:45 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Pandas groupby warning *** WARNING *** Larz60+ 0 803 Sep-30-2022, 04:38 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020