Python basics: how to check if a function returns multiple values?

The basic information I know ... ;-P But what is the best way to check if a function returns some values?

def hillupillu():
    a= None
    b="lillestalle"
    return a,b

if i and j in hillupillu(): #how can i check if i or j are empty? this is not doing it of course;-P
    print i,j 
0
source share
4 answers

If you meant that you cannot predict the number of return values ​​of a function, then

i, j = hillupillu()

will raise the value ValueErrorif the function does not return exactly two values. You can catch this with the usual construct try:

try:
    i, j = hillupillu()
except ValueError:
    print("Hey, I was expecting two values!")

This follows the general Python idiom of "asking for forgiveness, not permission." If it hillupillucan raise ValueError, you need to do an explicit check:

r = hillupillu()
if len(r) != 2:  # maybe check whether it a tuple as well with isinstance(r, tuple)
    print("Hey, I was expecting two values!")
i, j = r

, None , None in (i, j) if -clause.

+8

Python . , .

, :

tuple_ = hillupillu()
i = tuple_[0] if tuple_ else None
j = tuple_[1] if len(tuple_) > 1 else None
+6

After getting the values ​​from the function:

i, j = hillupillu()

You can check if the Nonestatement matters is:

if i is None: ...

You can also just check the truth value of the value:

if i: ...
0
source
if(i == "" or j == ""):
   //execute code

something like this should wordk, but if you give it a value None, you will do it

if(i == None or j == None):
    //execute code

hope that helps

0
source

Source: https://habr.com/ru/post/1687063/


All Articles