Anh
Anh

Reputation: 75

Pythonic way to initalize a boolean

Is this the best/most Pythonic way to initialize a boolean for this purpose?

if start == today:
    b = date_time_obj <= start
else:
    b = date_time_obj < start

if b:
    do_something()

I can't think of another way to do it. Thanks in advance.

Upvotes: 0

Views: 47

Answers (2)

DeepSpace
DeepSpace

Reputation: 81664

And if you really want to have some fun, try

b = (date_time_obj < start, date_time_obj <= start)[start == today]

Upvotes: 0

Booboo
Booboo

Reputation: 44283

Probably more common is:

b = date_time_obj <= start if start == today else date_time_obj < start

Upvotes: 1

Related Questions