When do default parameters get their values set?

Steven D'Aprano steve at pearwood.info
Wed Dec 10 22:15:09 EST 2014


On Wed, 10 Dec 2014 18:18:44 -0800, Rustom Mody wrote:


> 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'? 


Can *what* be demonstrated? That the functions returned are different 
objects, or that the dicts are different objects? Both? Something else?

Using "is" you are demonstrating that calling the function twice returns 
two distinct objects. That is the purpose of "is", to compare object 
identity. Without "is", you can compare object IDs directly:

id(f()()) == id(f()())

but that's ugly and less efficient. Using "is" is the more idiomatic and 
natural way to do this.


Other than that, you could do something to demonstrate the consequences 
of the two values being distinct objects:

a = f()()  # Call twice to get a dict.
b = f()()
a['key'] = 23
b['key']  # raises KeyError


or

a = f()  # Call once to get a function.
b = f()
a.attribute = 23
b.attribute  # raises AttributeError


Or you could inspect the byte-code of f and try to understand it.



> Because to me 'is' -- equivalently id -- is a code-smell 

It is only a code-smell in the sense that caring about object identity 
should be rare.



-- 
Steven



More information about the Python-list mailing list