How do I check all variables returned buy the functions exists

Rob Gaddi rgaddi at highlandtechnology.invalid
Fri Sep 15 12:08:16 EDT 2017


On 09/15/2017 08:43 AM, Ganesh Pal wrote:
> I have a function that return's x  variables  How do I check if all the the
> values returned  are not None/False/0/''
> 
> Here is the same program to demonstrate this , but I  felt this can be
> better  any suggestions ?
> 
> 
> # vi file.py
> import random
> import string
> 
> 
> def return_x_values():
>      " returns x strings for further processing"
>      value1 =  random.choice(string.ascii_letters)
>      value2 = random.choice(string.ascii_letters)
>      value3 = random.choice(string.ascii_letters)
>      return (value1, value2, value3)
> 
> 
> #unpack them
> 
> value1, value2 , value3 = return_x_values()
> 
> 
> # check if its not none
> 
> # I think this can be better
> if value1 and value2 and value3 :
>     print "continue with the program"
> 
> else:
>    print "Exting the program"
> 
> 
> # python file.py
> continue with the program
> 
> I  am a Linux user with Python 2.7
> 
> Regards,
> Ganesh
> 

Don't unpack them yet, you still want them to be aggregated.

vals = return_x_values()
if all(vals):
     v1, v2, v3 = vals
     print "all values true"
else:
     print "at least one false value"

-- 
Rob Gaddi, Highland Technology -- www.highlandtechnology.com
Email address domain is currently out of order.  See above to fix.



More information about the Python-list mailing list