Skip to main content
added 1 character in body
Source Link
user2555451
user2555451

Since None evaluates to False, you could do:

def func2(n): return func1(n) or something_else 

It should be noted however that this will cause func2 to return something_else if func1(n) returns anything falsey (0, [], etc.)


For many functions, you could use next and some generator expressions:

def func2myfunc(n): vals = (f(n) for f in (func1, func2, func3...)) return next((v for v in vals if v is not None), something_else) 

Since None evaluates to False, you could do:

def func2(n): return func1(n) or something_else 

It should be noted however that this will cause func2 to return something_else if func1(n) returns anything falsey (0, [], etc.)


For many functions, you could use next and some generator expressions:

def func2(n): vals = (f(n) for f in (func1, func2, func3...)) return next((v for v in vals if v is not None), something_else) 

Since None evaluates to False, you could do:

def func2(n): return func1(n) or something_else 

It should be noted however that this will cause func2 to return something_else if func1(n) returns anything falsey (0, [], etc.)


For many functions, you could use next and some generator expressions:

def myfunc(n): vals = (f(n) for f in (func1, func2, func3...)) return next((v for v in vals if v is not None), something_else) 
deleted 164 characters in body
Source Link
user2555451
user2555451

Since None evaluates to False, you could do:

def func2(n): return func1(n) or something_else 

It should be noted however that this will cause func2 to return something_else if func1(n) returns anything falsey (0, [], etc.)


For many functions, you could use next and nestedsome generator expressions:

def func2(n):   return next((x for xvals in= (f(n) for f in (func1, func2, func3...))   if x), something_else) 

But that is not very readable in my opinion. I think it would be best to use:

 return func1next(n)(v orfor func2(n)v orin func3(n)vals orif v is not None), something_else) 

for two to four functions and a loop for anything more.

Since None evaluates to False, you could do:

def func2(n): return func1(n) or something_else 

It should be noted however that this will cause func2 to return something_else if func1(n) returns anything falsey (0, [], etc.)


For many functions, you could use next and nested generator expressions:

def func2(n):   return next((x for x in (f(n) for f in (func1, func2...)) if x), something_else) 

But that is not very readable in my opinion. I think it would be best to use:

return func1(n) or func2(n) or func3(n) or something_else 

for two to four functions and a loop for anything more.

Since None evaluates to False, you could do:

def func2(n): return func1(n) or something_else 

It should be noted however that this will cause func2 to return something_else if func1(n) returns anything falsey (0, [], etc.)


For many functions, you could use next and some generator expressions:

def func2(n): vals = (f(n) for f in (func1, func2, func3...))    return next((v for v in vals if v is not None), something_else) 
added 3 characters in body
Source Link
user2555451
user2555451

Since None evaluates to False, you could do:

def func2(n): return func1(n) or something_else 

It should be noted however that this will cause func2 to return something_else if func1(n) returns anything falsey (0, [], etc.)


For many functions, you couldcould use next and nested generator expressions:

def func2(n): return next((x for x in (f(n) for f in (func1, func2...)) if x), something_else) 

But I'mthat is not a huge fan of this solution; it packs too much on one linevery readable in my opinion. I think it would be best to use:

return func1(n) or func2(n) or func3(n) or something_else 

for 2-4two to four functions tops and a loop for anything more.

Since None evaluates to False, you could do:

def func2(n): return func1(n) or something_else 

It should be noted however that this will cause func2 to return something_else if func1(n) returns anything falsey (0, [], etc.)


For many functions, you could use next and nested generator expressions:

def func2(n): return next((x for x in (f(n) for f in (func1, func2...)) if x), something_else) 

But I'm not a huge fan of this solution; it packs too much on one line. I think it would be best to use:

return func1(n) or func2(n) or func3(n) or something_else 

for 2-4 functions tops and a loop for anything more.

Since None evaluates to False, you could do:

def func2(n): return func1(n) or something_else 

It should be noted however that this will cause func2 to return something_else if func1(n) returns anything falsey (0, [], etc.)


For many functions, you could use next and nested generator expressions:

def func2(n): return next((x for x in (f(n) for f in (func1, func2...)) if x), something_else) 

But that is not very readable in my opinion. I think it would be best to use:

return func1(n) or func2(n) or func3(n) or something_else 

for two to four functions and a loop for anything more.

added 605 characters in body
Source Link
user2555451
user2555451
Loading
Source Link
user2555451
user2555451
Loading