On Thu, Mar 12, 2020 at 03:02:28PM -0000, Eric Wieser wrote:

> TL;DR: should we make `del x` an expression that returns the value of `x`.
> 
> ## Motivation
> 
> I noticed yesterday that `itertools.combinations` has an optimization 
> for when the returned tuple has no remaining ref-counts, and reuses it 
> - namely, the following code:
> 
>     >>> for v in itertools.combinations([1, 2, 3], 1):
>     ...    print(id(v))
>     ...    del v  # without this, the optimization can't take place
>     2500926199840
>     2500926199840
>     2500926199840


[...]
> My suggestion would be to make `del x` an expression, with semantics 
> "unbind the name `x`, and evaluate to its value".
> This would allow:
> 
>     >>> [id(del v) for v in itertools.combinations([1, 2, 3], 1)]
>     [2500926200992, 2500926200992, 2500926200992]

Pity that locals() is so weird, because this works fine in module level 
code, but not inside comprehensions or functions:

    py> for v in combinations([1, 2, 3], 1):
    ...     print(id(locals().pop('v')))
    ...
    3083202636
    3083202636
    3083202636

Mind you, I think that the cost of calling locals().pop is going to 
outweigh any hypothetical saving you get from reusing the object.


-- 
Steven
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/[email protected]/message/PVXSVENZBLAHZA6TFPYJRAR522RR7FXP/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to