Expression can be simplified on list
Thomas 'PointedEars' Lahn
PointedEars at web.de
Mon Sep 12 17:21:39 EDT 2016
Chris Angelico wrote:
> On Mon, Sep 12, 2016 at 9:14 PM, Daiyue Weng <daiyueweng at gmail.com> wrote:
>> Hi, I found that when I tried to make an equality test on empty like,
>>
>> if errors == []:
>>
>> PyCharm always warns me,
>>
>> Expression can be simplified.
>>
>> I am wondering what's wrong and how to fix this?
>>
>
> If you know that 'errors' is always going to be a list, you can check
> for emptiness thus:
>
> if not errors:
If I knew that it is always going to be a list or a tuple, I would check its
length instead:
if len(errors) == 0:
Only if I did not know that I would use the “not” keyword, because the
latter
a) forces a type conversion to “bool”;
b) also works with values of other types.
> PyCharm recommends this syntax rather than the comparison with a
^^^^^^^^^^^
> newly-created empty list.
Are you sure?
> Your code has to actually construct and dispose of a list, just for the
> comparison.
ACK
--
PointedEars
Twitter: @PointedEars2
Please do not cc me. / Bitte keine Kopien per E-Mail.
More information about the Python-list
mailing list