Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

expression can be simplified on boolean literal [duplicate]

I have the following code that reproduces a PyCharm warning,

Expression can be simplified

This expression detects equality comparison with a boolean literal.

seq_group = []
if seq_group == []: # warning here
   print("it is empty.")

if I change the code to,

if seq_group is None:

will fix the warning, but the real question is are None and []emplty list the same thing?

cheers

like image 335
daiyue Avatar asked Sep 28 '16 11:09

daiyue


1 Answers

are None and [] empty list the same thing?

Nope, and it will result in erroneous behavior:

seq_group = []

if seq_group is None:
    print("it is empty")

This doesn't print anything, None is completely different to [], value and identity wise. None indicates the absence of a value, [] indicates a list with no values. The confusion might arise from the fact that both happen to evaluate False in conditionals.

The warning is probably due to the fact that you can simply use seq_group with not instead of using a literal with ==:

if not seq_group:
    print("it is empty")
like image 155
Dimitris Fasarakis Hilliard Avatar answered Sep 22 '22 02:09

Dimitris Fasarakis Hilliard