+ 1
How to disable SyntaxWarning in a bit?
I have this syntax warning every time when I run my bit: "file0.py:95: SyntaxWarning: "is" with a literal. Did you mean "=="?" How can I disable it, do not changing my code?
16 Respostas
+ 1
LoooooooL
nothing can be done. The warning seems to be non-suppressible. Maybe use earlier Python versions?
"""
msg358775 - (view)Author: Serhiy Storchaka (serhiy.storchaka) *Date: 2019-12-21 18:18It cannot be suppressed by the code in the module caused a warning, because the warning is emitted at compile time. You need to silence it before compiling that module. Once you have compiled it, warning will no longer be emitted.
"""
https://bugs.python.org/issue34850
+ 9
You should probably not ignore this warning. Whatchout for the difference between "is" and "==".
you usually get this warning when comparing values with is.
x = 24
if x is 24:
print("is 24")
is: for comparing identity, it's useful for comparing instances
==: for comparing equality, it's useful for comparing values.
+ 7
You will not be able to disable it. However you can "hide" it using try/except keywords. This is not recommended though. You can find more on this in python documentation
+ 5
LoooooooL please share your code
+ 5
import warnings
warnings.simplefilter("ignore")
that's all
+ 2
Apollo-Roboto Thanks, I know differents between "==" and "is". I just want to disable this warning in output.
+ 2
LoooooooL oh right, remove the try/except block of code. Apollo-Roboto is correct. Change ~line 100 to: **if luck == 'SECRET':**
Because you need to compare values, not identity
+ 2
LoooooooL
luck is 'SECRET'
"I did it on purpose"
But why? It is the wrong thing to do, that's why you're getting the warning. You should be using
luck=='SECRET'
you think they are the same. they are not the same.
Explain your idea, I am sure it is very interesting.😁.
Snehil
But you shouldn't encourage him to shoot himself in the foot.😅
+ 2
no. unfortunately.
+ 1
Bob_Li I answered him as per requirements, he clearly mentioned that he doesn't want his code to be changed and about 'shooting himself in foot' he will learn something new from what I gave. I'm not encouraging him to ignore warnings
+ 1
Snehil 😁 you're right. Sometimes the best way of learning is the hard way.
+ 1
As a solution I've hided this warning output using this code in last line:
print('\n'*32)
0
Snehil Lamron Unfortunately, none of this works here in SoloLearn Code Bits.
Sakshi Here is my bit:
https://code.sololearn.com/c4EB9UvE76nC/
There is a hint in the code that the program can be hacked using a cheat code. But this is a trick, because it will not work. In previous versions of the interpreter, there were no SyntaxWarning error messages. And now, this little prank is immediately issued when the program starts.
0
Lamron I don't know if I was able to explain the idea in the last post. I did it on purpose.
0
I'll try to explain again. I'm also adding my previous post to keep things consistent.
https://code.sololearn.com/c4EB9UvE76nC/
There is a hint in the code speaking that the program can be "hacked" using a "cheat code". But this is a trick, because it will not work. In previous versions of the interpreter, there were no SyntaxWarning error messages. And now, this little prank is immediately issued when the program starts.
I want you to understand that we are on an educational platform. The code I write here is also written for educational purposes.
In my snippet, I intentionally used "is" instead of "==", in one of the options. If you delve into reading the code and comments, then you can understand why I use this particular literal.
With this trick (intentionally created error), I want to demonstrate how "==" differs from "is". But I do it in a hidden, non-obvious way, forcing the reader of the code to figure out for himself why the code does not work in a way that seems obvious.
And it's kind of a Easter egg that's hidden in this code. But the output of this error immediately reveals this Easter egg, which spoils all joy. The output of this error is a kind of spoiler which I want to prevent.
I hope I explained my idea in sufficient detail :)