2 ms·
That's not really what's going on. Python has default values where, say Ruby, has default expressions. def myfunc(x=[]) puts x end In Ruby is th
by Spivak 3y ago
That's not really what's going on. Python has default values where, say Ruby, has default expressions.
def myfunc(x=[])
puts x
end
In Ruby is the same as
def myfunc(x=lambda:[])
print(x())
in Python when the default is needed. The benefit is performance, you only have to evaluate the default once at function definition time. I won't say it's not a footgun but it's also I think a sane choice when in Python it's common to have functions with many default parameters.
- o1y32 3y ago"the benefit is its performance" Sure, but when you design a language you are striking a balance between many things (which is why some features could take years to finalize and implement). In this case, the gain in performance is likely minimal, but the confusion and surprise it causes does a lot of damage. (I recently read through the entire series of conversation with Anders Hejlsberg on the design of C# which is very insightful https://www.artima.com/intv/anders.html https://www.artima.com/intv/anders.html)