[New-bugs-announce] [issue38062] Clarify that atexit.unregister matches by equality, not identity

Mark Dickinson report at bugs.python.org
Mon Sep 9 04:51:28 EDT 2019


New submission from Mark Dickinson <dickinsm at gmail.com>:

Suppose I have a class that looks like this:

    class A:
        def cleanup(self):
            print("Doing essential cleanup")

and on an instance `a = A()`, I do: `atexit.register(a.cleanup)`.

Then it's not obvious from the documentation that an `atexit.unregister(a.cleanup)` will successfully undo the effect of the reigster call: the second `a.cleanup` is a different object from the first:

    >>> a = A()
    >>> clean1 = a.cleanup
    >>> clean2 = a.cleanup
    >>> clean1 is clean2
    False

Luckily, though the two bound methods are different objects, they're equal:

    >>> clean1 == clean2
    True

and from looking at the source, it's apparent that `atexit.unregister` compares by equality rather than identity, so everything works.

It would be good to add a sentence to the documentation for `atexit.unregister` to clarify that this can be expected to work.

----------
messages: 351363
nosy: mark.dickinson
priority: normal
severity: normal
status: open
title: Clarify that atexit.unregister matches by equality, not identity

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue38062>
_______________________________________


More information about the New-bugs-announce mailing list