2 ms·
Can you (or someone) clarify what problem you're talking about? This seems to work as expected for me: def foo(x=[]): sum = 0 for a in x:
by throwaway91919 11y ago
Can you (or someone) clarify what problem you're talking about? This seems to work as expected for me:
def foo(x=[]):
sum = 0
for a in x: sum += a
return sum
print foo([3,5]) # 8
print foo(x=[3]) # 3
print foo([]) # 0
print foo() # 0
Is it the fact that kwargs are required to have defaults? Or what?
- raverbashing 11y agoNah, do it like this def foo(i,x=[]): x.append(i) return x foo(1) foo(2) See what happens
- saltylicorice 11y agoThe problem is that the default argument is mutable. def f(a=[]): a.append('v') print a f(['ok']) # ['ok', 'v'] f() # ['v'] f() # ['v', 'v']
- throwaway91919 11y agoAh, thanks.