I have a function f
that takes in the arguments i
, A
and B
. i
is a counter and A
and B
are lists or constants. The function just adds the i-th element of A
and B
if they are lists. Here's what I have written in Python 3.
def const_or_list(i, ls):
if isinstance(ls, list):
return ls[i]
else:
return ls
def f(i, A, B):
_A = const_or_list(i, A)
_B = const_or_list(i, B)
return _A + _B
M = [1, 2, 3]
N = 11
P = [5, 6, 7]
print(f(1, M, N)) # gives 13
print(f(1, M, P)) # gives 8
You will notice that const_or_list()
function is called on two (but not all) of the input arguments. Is there a decorator (presumably more Pythonic) approach to achieve what I am doing above?