When do default parameters get their values set?

Steven D'Aprano steve+comp.lang.python at pearwood.info
Thu Dec 11 17:51:01 EST 2014


Chris Kaynor wrote:

> On Wed, Dec 10, 2014 at 7:15 PM, Steven D'Aprano <steve at pearwood.info>
> wrote:

>> 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.
>>
> 
> In CPython, that does not work, as the dictionary will be garbage
> collected after each call to id:
>>>> f = lambda : (lambda x= {}: x)
>>>> f()() is f()()
> False
>>>> id(f()()) == id(f()())
> True


Nice catch! Thank you for the correction.



-- 
Steven




More information about the Python-list mailing list