简体   繁体   中英

How can I handle this PEP8 warning?

I have a two-line code like this:

if validated_data.get('is_shipped') == False:  # 管理员取消了发货,则把物流信息删掉
    validated_data.update(logistics_company=None, logistics_tracking_no=None)

I am using Pycharm and the PEP8 check is on. So there is an underline below validated_data.get('is_shipped') == False which shows me Expression can be simplified. Replace boolean expression with ``not validated_data.get('is_shipped')`` Expression can be simplified. Replace boolean expression with ``not validated_data.get('is_shipped')`` . But, the truth is, I only want to run code in if statement when is_shipped is explicitly passed as False , and not when is_shipped is None . So, how can I handle this PEP8 check warning?

You hit the solution in your question: use the explicit is operator:

if validated_data.get('is_shipped') is False:

This makes your condition an explicit identity, rather than the "truthy" boolean evaluation.

Here x == False is equivalent to not x , and I don't know why do you want to only use x == False and PEP8 suggests that:

if not validated_data.get('is_shipped'):  # 管理员取消了发货,则把物流信息删掉
    validated_data.update(logistics_company=None, logistics_tracking_no=None)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM