calling __del__ [was Re: [Tutor] beginning to code]

Steve D'Aprano steve+python at pearwood.info
Mon Sep 25 12:55:57 EDT 2017


On Sun, 24 Sep 2017 09:13 am, Bill wrote:

[context snipped for brevity]
> I agree (I was a bit hasty in my choice of words); but if they didn't
> "have" these references, it would be difficult, though not impossible,
> to refer to them.   Also keep in mind that the garbage collector keeps
> track, generally, of how many there are for each object!   So from the
> gc's point of view, objects (definitely) "have" references.

Unfortunately there's some ambiguity in the English word "have". Webster's
dictionary (1913) lists at least twelve distinct meanings. WordNet lists
nineteen.

If we consider "have" in the sense of possession, then objects don't have
references. If they did, you could ask an object what names they were known as,
and they could tell you:

# this doesn't work!
a = 99
b = c = a
a.names()
# returns ['a', 'b', 'c']


You can't do that in Python.

But in another sense, ironically one which isn't listed by either Webster's or
WordNet, objects do have references. The closest meaning I see is:

    7: have a personal or business relationship with someone; "have
       a postdoc"; "have an assistant"; "have a lover"

Its not a *personal* or *business* relationship, but there is a relationship
between the reference and the object.


> Next, what will Egg.__del__() do? : )

For the record, you shouldn't call dunder ("Double UNDERscore") methods directly
unless you know what you're doing. You should consider them for Python's use
only, unless explicitly told differently.

Calling __del__ directly is *especially* fraught with problems. It does not
delete the object. It just runs the destructor, but leaving the object alive
but in a potentially unusable state.


-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.




More information about the Python-list mailing list