[Python-Dev] performance of {} versus dict(), de fmd(**kw): return kw trumps all ; -)
Richard Oudkerk
shibturn at gmail.com
Thu Nov 15 17:45:42 CET 2012
On 15/11/2012 4:21pm, Terry Reedy wrote:
> I was thinking that CPython could check the ref count of the input
> keyword dict to determine whether it is newly created and can be
> returned or is pre-existing and must be copied. But it seems not so.
>
> >>> def d(**x): return sys.getrefcount(x)
>
> >>> import sys
> >>> d(a = 3)
> 2
> >>> d(**{'a': 3})
> 2
> >>> b = {'a': 3}
> >>> d(**b)
> 2
>
> I was expecting 3 for the last one.
Isn't it always newly created?
>>> def f(**x): return x
...
>>> b = {'a':3}
>>> b is f(**b)
False
--
Richard
More information about the Python-Dev
mailing list