PyCharm: “Simplify Chained Comparison” [duplicate] - python

This question already has answers here:
Simplify Chained Comparison
(2 answers)
Closed 5 years ago.
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.

Your expression can be rewritten as:
if cnt_1 < 0 <= cnt_2:
This is called comparison chaining.

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.

Related

Shouldn't "a:1" be a syntax error in python? [duplicate]

This question already has answers here:
What is this odd colon behavior doing?
(2 answers)
Closed 3 years ago.
I made a typo in my code that went completely silent syntactically.
dict_args : {"arg1":1,"arg2":2,"arg3":3}
# .... Some more code
some_function(**dict_args)
# .... Some more code
If you haven't noticed it, it's the use of : instead of = when declaring the variable dict_args.
So my question is, does the python syntax : a:1, by itself, hold any meaning ? Or should it hypothetically be considered a syntax error?
PEP-526 introduced variable annotations, which provide programmers a way to add type information to variables. This allows, among other things, statements like
x: int
to indicate that there is a local variable of type int, without initializing it. In PEP-484 - Acceptable Type Hints, we can see that annotations "must be valid expressions that evaluate without raising exceptions", which your dictionary literal is.
If you look at the Python grammar itself you can see that the expr_stmt and annassign rules make the example you show legal.
If you're using an IDE/other type hinting tools, they should definitely complain about this, but it doesn't break the rules that Python has set up.

When to use If If over an If Elif in Python [duplicate]

This question already has answers here:
If and elif in Python for good programming practices
(2 answers)
Closed 4 years ago.
Does someone have a simple answer as to why you would use a If If rather than an If Elif?
Example:
def fun(item):
if item[0]<'M':
return 0
if item[0]<'Q':
return 1
return 2
With a setup like this, where you are returning a value inside each if block, the if statements behave exactly the same as an if elif structure since the return exits out of the function without checking the conditions below. So, when the functionality is the same, the choice should be made on readability. explicit is better than implicit.
There is no good reason to use if statements like this implying exclusively in the conditionals, when if elif statements makes the exclusivity explicit and is much more readable and easier to maintain.

Proper Python Syntax and Semantics: if, else, pass [duplicate]

This question already has answers here:
Using pass on a non necessary else statement
(5 answers)
Closed 8 years ago.
Is there a preferred/proper style?
This:
def fx(Boolean):
if Boolean:
# Do stuff.
else:
pass
Or this:
def fx(Boolean):
if Boolean:
# Do stuff.
Is it preferred/proper to include else: pass if you don't want anything to happen?
I've read PEP 8 - Style Guide for Python Code and did not find anything concerning my question.
You should never include else: pass. It's superfluous. Just omit the else; it's intentionally an optional keyword.
If you don't need the else if there is no reason to add it. It will just confuse other people reading your code in the future (ie yourself a few months later).

'Safe' Assignment in MATLAB [duplicate]

This question already has answers here:
How do I do multiple assignment in MATLAB?
(9 answers)
Closed 9 years ago.
I've noticed that in a python code you can do something like:
a=0
b=1
a,b=b,a
print(a,b)
which outputs (a=1,b=0) (i.e. each variable is assigned independently of the last assignment). Is there a way to do something similar in MATLAB?
Sorry if this is a really simple question, but I've been trying to find a clean answer to this for a bit of time now and haven't found anything.
There is no need for an additional temporary variable here. If you want multiple assignments in a single statement, you can use deal:
[a, b] = deal(b, a)
I believe this is what you're looking for.
It's always possible to do that with a temporary variable in any language. The unpacking method of Python is just a little syntax sugar to simplify the developer life :)
a = 0
b = 1
tmp = a
a = b
b = tmp
print(a,b)
Anyway, it's not magical, the Python byte code might implement the permutation with a temporary variable. (There's techniques to do this without temp variable, but well... for the sake of clarity use one :p)

Python looping PEP8 rule [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Is it possible to implement a Python for range loop without an iterator variable?
just want to do something multiple times so I write
for i in range(1, 10):
print 'something'
print 'do not use i'
so when I run PyLint it says : "Unused variable 'i'" of course.
So what would be correct way to write this type of loop where I just want to loop multiple times.
i = 0
while i < 10:
print 'test'
i += 1
is this only way?
While you could substitute i with _, writing the loop as you have in the first place is perfectly fine, and writing it any other way would probably be considered strange to most Python programmers.
PyLint is checking too aggressively, and sometimes it should be ignored. Perhaps consider pyflakes if what you want is a checker that tells you if your code is likely to break; or pep8 if you want your style checked (variable naming etc).
The for i in range(..) loop (use xrange() in python2 though) is the correct way to loop over a range of numbers in python. The pylint warning should be ignored in this case. However, in case of an unused variable it's often named _ to make it clearer that it's not used.
While there are other ways to loop those are less efficient since they usually involve function calls. Besides that doing so would just not be what people expect to see when reading your code.
The version of PyLint I'm using right now oddly enough doesn't seem to cry about unused variables in this particular example. Be that as it may, there are several options:
Work with the iterator directly:
i = iter(range(1, 10))
try:
while i.next():
pass
except StopIteration:
pass # safely ignore
Add a PyLint comment at the top of your file to disable the warning:
# pylint: disable=X0123
# NOTE: X0123 is just an example, I don't know the exact warning code
Potentially append an _ to the unused variable. I'm not sure whether or not this naming convention is respected, but it is mentioned in the official user manual:
In our project we just accepted that we have to make some modifications in our code to please PyLint:
stick to more naming conventions (unused variables ending in underscores, ...)

Categories

Resources