On Fri, Mar 13, 2020 at 5:16 AM Marco Sulla email@example.com wrote:
On Thu, 12 Mar 2020 at 19:05, Chris Angelico firstname.lastname@example.org wrote:
The action of deleting a *name* is not the same as disposing of an *object*. You can consider "del x" to be very similar to "x = None", except that instead of rebinding to some other object, it unbinds x altogether.
Exactly. But if `x = None` will return the previous value of x, the refcount of the object will be not decreased, and Python does not free the object memory. An example:
(venv) marco@buzz:~/sources$ python Python 3.8.2 (tags/v3.8.2-dirty:7b3ab5921f, Mar 1 2020, 16:16:55) [GCC 9.2.0] on linux Type "help", "copyright", "credits" or "license" for more information.
from sys import getrefcount a = "nfnjgjsbsjbjbsjlbslj" getrefcount(a)
I don't understand your point. What you're showing here is that the name binding 'a' has a reference to that string, and then getrefcount itself has one (because the value is "in use" by being a parameter), and then you add another one using the REPL's "_" binding, at which point there are three references. What has this to do with the behaviour if something returned a value?
The broad idea of "del x" returning a value isn't inherently ridiculous. If you consider name bindings to be like a dictionary (and, in fact, it will often be implemented with one), then "del x" is very similar to "namespace.pop('x')", which returns the value that it's just removed. The semantics, in terms of whether an object can be garbage collected, would be identical. When the interpreter dels a name, it simply removes one reference.