Python Forum
how to silence this warning?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to silence this warning?
#1
how to silence this warning?
Output:
lt1a/forums/2 /home/forums 5> box foo.py +----<foo.py>---------------------+ | def foox(v): | | if v is 1: | | print('v is 1',repr(v)) | | if v == 1: | | print('v == 1',repr(v)) | | return | | foox(True) | | foox(1) | +---------------------------------+ lt1a/forums/2 /home/forums 6> python3.8 foo.py foo.py:2: SyntaxWarning: "is" with a literal. Did you mean "=="? if v is 1: v == 1 True v is 1 1 v == 1 1 lt1a/forums/2 /home/forums 7>
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
Perhaps write if operator.is_(v, 1): .... Or if id(v) == id(1): ...
Skaperen likes this post
Reply
#3
(Nov-11-2022, 01:10 PM)Gribouillis Wrote: Perhaps write if operator.is_(v, 1): .... Or if id(v) == id(1): ...
both of those worked. thanks!
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
This SyntaxWarning was new in Python 3.8
The reason behind it case 34850.
Serhiy Storchaka Wrote:Gregory have noticed that the is and is not operator sometimes is used with string and numerical literals.
This code works on CPython by accident,because of caching on different levels (small integers and strings caches, interned strings, deduplicating constants at compile time).
But it shouldn't work on other implementations, and can not work even on early or future CPython versions.
Reply
#5
(Nov-11-2022, 06:36 PM)snippsat Wrote: The reason behind it case 34850.
In this bug's description, it is said that
Quote: using "is" with a literal is always a mistake
So perhaps Skaperen should not use "is" with a literal.
snippsat likes this post
Reply
#6
i can certainly use the id(a) == id(b) code Gribouillis first posted. is that a mistake, too?

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. if it is considered a mistake to be doing things where 1 and True are to be considered as different values, then (IMHO) having the boolean type have different values should be considered a mistake. then, if you want to use True, you would do True = 1 at the start of your code.

or maybe not have a boolean type at all. i have coded in more than one language that did not have one (and one that did but did not treat True == 1 as true).
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#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
#8
i was hoping to see some better definition as in how the language is defined. as for the case of True vs 1 (the actual thing i ran into) i suspected the possible answer was to separate the cases by type, first, typically with is instance(), though there are other ways to do it. even the id() way won't find x and y to be the same when separately assigned from large int literals. there seem to be 3 different questions we can ask of x and y:
1. are x and y equal?
2. are x and y exactly alike?
3. are x and y (references to) the same object?
question #2 seems to be what i was asking while "is" is meant for question #3. i ran into this whole issue with code if output is 1 which i should recode more like if isinstance(output,int) and output == 1.

i would not call this a syntax error. instead, i would call this a semantic ambiguity based on the meaning of it; not based on where a particular implementation detects it.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Pandas groupby warning *** WARNING *** Larz60+ 0 758 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