[Tutor] Confirmation if command worked

Steven D'Aprano steve at pearwood.info
Thu Aug 25 15:27:42 CEST 2011


Steven D'Aprano wrote:

> if some_condition:
>     flag = True
> else:
>     flag = False
> 
> is better written as:
> 
> flag = some_condition


Actually, that's a slight over-simplification.

some_condition may not actually be a bool. If you don't mind flag also 
being a non-bool, that's fine, but if you want to ensure that it is one 
of True/False, then you can call:

flag = bool(some_condition)

to force flag to be one of True or False. But that's generally not 
necessary unless you care about what flag looks like when printed.



-- 
Steven


More information about the Tutor mailing list