Steve D'Aprano <steve+pyt...@pearwood.info> writes: > On Sun, 4 Sep 2016 10:37 pm, Ben Finney wrote: > > > Short anser: because nothing has removed the reference to the > > instance. > > Hmmm. You're probably right, but not for the reason you think :-)
Thanks to those who corrected some details in my explanation. > > This is a good example of why it helps to *never* call ‘__init__’ > > the “constructor”. It isn't, because ‘__init__’ acts on an > > already-constructed instance. > > I don't believe I did call it the constructor :-) You have advocated that in the past, though :-) I am presenting this as a counter-example: ‘__init__’ does not act as the constructor, so it's needlessly misleading to refer to it that way. > > Right. The instance is constructed successfully (by ‘__new__’, the > > constructor). It will be bound to ‘s’, creating a reference to the > > instance. > > Bound to `s`? Did you mean `e`? I did. And yes, that was incorrect: the assignment statement does not complete because the exception interrupts it. So ‘e’ is not a reference to the instance. > So it looks like what may have been holding onto the reference to the > Eggs instance was the exception or traceback, and once that got > garbage-collected (by me causing a new exception and traceback) the > finaliser ran. > > Which both Oscar and Chris suggested, thanks guys! Indeed. Thanks for dissecting the right and wrong parts :-) The larger point which remains true: Don't think of ‘__init__’ as a constructor, all it does is work on an *already-constructed* instance. The ‘__new__’ method is the constructor. The instance doesn't exist before that method is called; if successful, the instance is constructed and returned. The ‘__init__’ method is the initialiser. It receives the already-constructed instance, and returns ‘None’. That doesn't affect existing references to the instance. -- \ “Shepherds … look after their sheep so they can, first, fleece | `\ them and second, turn them into meat. That's much more like the | _o__) priesthood as I know it.” —Christopher Hitchens, 2008-10-29 | Ben Finney -- https://mail.python.org/mailman/listinfo/python-list