Why doesn't my finaliser run here?

Chris Angelico rosuav at gmail.com
Sun Sep 4 22:10:58 EDT 2016


On Mon, Sep 5, 2016 at 11:48 AM, Ben Finney <ben+python at benfinney.id.au> wrote:
> 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.

It doesn't really matter, though. Here's the outside view of
constructing a class:

instance = Class()

Doesn't matter how it's implemented; you call something and you either
get an instance back, or catch an exception.

While it's technically true that __init__ raising an exception won't
affect existing references to that instance, it's almost immaterial.
You generally won't implement __new__ to retain refs, and you
generally won't call __init__ again after the instance has been fully
constructed. Generally, you can assume that an exception in __init__
means you can safely dispose of the object.

ChrisA



More information about the Python-list mailing list