Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python chained comparison [duplicate]

Tags:

python

django

I have this code:

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

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?

like image 292
Prometheus Avatar asked Mar 21 '26 09:03

Prometheus


1 Answers

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!')
like image 112
Ffisegydd Avatar answered Mar 23 '26 01:03

Ffisegydd



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!