
On Fri, Mar 13, 2020 at 6:53 AM Marco Sulla <mail.python.org@marco.sulla.e4ward.com> wrote:
On Thu, 12 Mar 2020 at 19:35, Chris Angelico <rosuav@gmail.com> wrote:
I don't understand your point.
Yes, Andrew Barnert already explained me that :)
The broad idea of "del x" returning a value isn't inherently ridiculous
The point is Eric Wieser does not really want this. What he want is something similar to the numpy patch he linked: https://github.com/numpy/numpy/pull/7997
that is an automatic delete of temporary, large objects. For example:
z = a + b + c + d
will create a temporary (a+b) object and a temporary (a + b) + c object. With that patch, if a, b, c and d are all ndarrays, the temporary objects are discard as soon as they are no more referenced, instead of at the end of the statement.
They actually ARE already discarded, as can be seen with a simpler example: class Thing: def __init__(self, name): self.name = name print("Creating", self) def __repr__(self): return '%' + self.name + '%' def __add__(self, other): return type(self)(self.name + other.name) def __del__(self): print("Garbage collecting", self) a = Thing("a") b = Thing("b") c = Thing("c") d = Thing("d") z = a + b + c + d print(z) The "ab" object gets disposed of before "abcd" gets created.
He thought that the change of `del` he proposed will give him that behavior, but this is not true. And I don't think it's very much simple to implement this feature for __all__ Python objects. Maybe for immutable ones, but it should be done one by one, I suppose, since there's not an "immutable iterface".
Yeah. The only way you could really optimize this would be to notice that the "ab" object has only one reference, and so it can be mutated. But that would be pretty finicky, and it would depend entirely on refcounting semantics (which are not a guarantee of the language - a mark-and-sweep GC is 100% valid, and wouldn't be able to do this optimization). Optimizing this CAN theoretically be done, but it's probably a lot simpler and cleaner to just use in-place modifications (eg augmented assignment) explicitly. ChrisA