Python链式比较 [英] Python chained comparison

查看:51
本文介绍了Python链式比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有此代码:

if self.date: # check date is not NoneType
    if self.live and self.date <= now and self.date >= now:
         return True
return False

我的IDE表示:看起来应该简化它,即Python链式比较.

My IDE says: This looks like it should be simplified i.e. Python chained comparison.

什么是链式比较,如何将其简化?

What is a chained comparison and how can it be simplified?

推荐答案

链式比较的示例如下所示.

An example of a chained comparison is shown below.

age = 25

if 18 < age <= 25:
    print('Chained comparison!')

请注意,这实际上是完全相同如下所示,它看起来更好.

Note that beneath the covers this is exactly the same as shown below, it just looks nicer.

age = 25

if 18 < age and age <= 25:
    print('Chained comparison!')

这篇关于Python链式比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆