3 ms·
Regarding the code: l = [] def f(param = l): pass def g(param = []): pass I would say f should be a syntax error, as languages shouldn't allow a default
by cabalamat 18y ago
Regarding the code:
l = []
def f(param = l): pass
def g(param = []): pass
I would say f should be a syntax error, as languages shouldn't allow a default parameter to be an expression. If people wanted that behavour, they could write:
def f(param =[]):
if param==[]: param = l
#...etc...
- jerf 18y agoIn Python, [] is an expression too; it is an expression that constructs an empty list and returns it. Again, if you change this just for argument lists, you're looking at another wart. I'm not disagreeing with you. Your logic makes sense in isolation. However, language design invariably requires this sort of tradeoff.