Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

return value in Python

I would like my function to return 1, 2, or 3 values, in regard to the value of bret and cret. How to do something more clever than this ? :

def myfunc (a, bret=False, cret=False):
  b=0
  c=0
  if bret and cret:
   return a,b,c
  elif bret and not cret:
   return a,b
  elif not bret and cret:
   return a,c
  else:
   return a
like image 472
Basj Avatar asked Dec 16 '22 03:12

Basj


2 Answers

How about:

def myfunc(a, bret=False, cret=False):
   ...
   return (a,) + ((b,) if bret else ()) + ((c,) if cret else ())
like image 69
NPE Avatar answered Dec 21 '22 22:12

NPE


def myfunc (a, bret=False, cret=False):
  b, c, ret = 0, 0, [a]
  if bret: ret.append(b)
  if cret: ret.append(c)
  return ret
like image 40
thefourtheye Avatar answered Dec 22 '22 00:12

thefourtheye