Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PyCharm: “Simplify Chained Comparison” [duplicate]

Tags:

python

pycharm

I have two integer value cnt_1 and cnt_2, and I write the following statements:

if cnt_1 < 0 and cnt_2 >= 0:
    # some code

This statement gets underlined, and the tooltip tells me that I must:

simplify chained comparison

As far as I can tell, that comparison is about as simple as they come. What have I missed here?

The question is a little different from link, there are different variables in comparison.

like image 332
Jianpeng Hou Avatar asked Jun 20 '17 17:06

Jianpeng Hou


2 Answers

Your expression can be rewritten as:

if cnt_1 < 0 <= cnt_2:

This is called comparison chaining.

like image 118
Błotosmętek Avatar answered Sep 18 '22 02:09

Błotosmętek


Pycharm is trying to tell you that the equation can be simplified. If you want to know what PyCharm would prefer it to be, PyCharm will help automate this fix. If you navigate your cursor to the underlined code and do:

Alt + Enter -> 'Simplify chained expression'

PyCharm will change this to:

if cnt_1 < 0 <= cnt_2:

The warning will now be gone. If you prefer the original code and are just wanting the warning to go away you can place your cursor on the warning and do

Alt + Enter -> 'Ignore...'

And this type of error will no longer be flagged. You can also access both of these options on a global scale by doing.

Code->"Inspect code..."-> (Choose the scope you'd like to inspect) -> Ok

This will give you a list of all the warnings in the scope you've selected and provide you with an automated method to fix many of them.

like image 36
nanotek Avatar answered Sep 21 '22 02:09

nanotek