[Python-ideas] Allow "assigning" to Ellipse to discard values.
Chris Angelico
rosuav at gmail.com
Tue Feb 10 23:28:46 CET 2015
On Wed, Feb 11, 2015 at 8:33 AM, <random832 at fastmail.us> wrote:
> On Tue, Feb 10, 2015, at 16:23, Chris Barker wrote:
>> granted, the _ sticks around and keeps the object alive, which may cause
>> issues. But is it often a big deal?
>
> Is cpython capable of noticing that the variable is never referenced (or
> is only assigned) after this point and dropping it early? Might be a
> good improvement to add if not.
CPython? Seems pretty unlikely. Other Pythons, like PyPy? Maybe... but
only for types without __del__. Consider:
class ReportDestruction:
def __init__(self, msg): self.msg = msg
def __del__(self): print("DESTROY:", self.msg)
def f():
x = ReportDestruction("on leaving f()")
ReportDestruction("Immediate")
... other code ...
In current CPython, the second reporter will speak immediately, and
the first one will speak when the function terminates, unless the
"other code" creates a refloop, in which case it'll speak when the
loop's broken (eg by gc.collect()). It's perfectly possible for object
lifetimes to be _extended_. However, I don't know of any way that you
could - or would ever want to - _shorten_ the lifetime of an object.
There'll be a lot of code out there that assumes that objects stick
around until the end of the function.
For something like PyPy, which already has special cases for different
data types, it's reasonable that some might be disposed of early
(especially simple strings and integers, etc). But I doubt CPython
will get anything like this in the near future. It's a dangerous
optimization.
ChrisA
More information about the Python-ideas
mailing list