If statement with or operator

Rhodri James rhodri at kynesim.co.uk
Wed Feb 22 13:56:38 EST 2017


On 22/02/17 17:38, Peter Pearson wrote:
> On Wed, 22 Feb 2017 22:33:31 +0530, Ganesh Pal wrote:
> [snip]
>> I  need suggestion on the if statement in the below code  , all that I was
>> trying to do was to add a check  i.e if any one of the functions return
>> True then  break the loop.
>>
>>  end_time = time.time() + 300
>>     umount_completed = False
>>     while time.time() < end_time:
>>         if attempt_umount() is True or df_output_lines() is True:
>>            logging.info("umount completed sucessfully")
>>            umount_completed = True
>>            break
>>         time.sleep(15)
>>     assert umount_completed, "ERROR: The umount failed Exiting"
>
> The if statement is healthy and reasonable.

Quite to the contrary, the if statement should be discouraged with great 
vigour unless the OP is certain that the functions return only the 
object True for success, and not just values that are true in a boolean 
context.

   if attempt_umount() or df_output_lines():

is far more likely to do what you expect.


-- 
Rhodri James *-* Kynesim Ltd



More information about the Python-list mailing list