When do default parameters get their values set?
Rustom Mody
rustompmody at gmail.com
Wed Dec 10 21:18:44 EST 2014
On Thursday, December 11, 2014 12:09:10 AM UTC+5:30, rand... at fastmail.us wrote:
> On Tue, Dec 9, 2014, at 21:44, Rustom Mody wrote:
> > Nice example -- thanks.
> > Elaborates the why of this gotcha -- a def(inition) is imperative.
> > From a semantic pov very clean.
> > From an expectation pov always surprising.
>
> Of course, I used a lambda for this. The equivalent without would be:
>
> def f():
> def g(x={}):
> return x
> return g
Ok. As I wrote in the "Question on lambdas" thread yesterday
lambdas and defs are equivalent
And going the other way -- no defs only lambdas its this:
>>> f = lambda : (lambda x= {}: x)
>>> f()() is f()()
False
>>> d = f()
>>> d() is d()
True
>>>
But I have a different question -- can this be demonstrated without the 'is'?
Because to me 'is' -- equivalently id -- is a code-smell and is like
explaining funny behavior by showing the dis -- like
$ gcc -S ...
-- output.
It can always explain, but indicates that the semantics is not (sufficiently) abstract in this aspect
More information about the Python-list
mailing list