This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author serhiy.storchaka
Recipients quapka, serhiy.storchaka, terry.reedy
Date 2021-12-20.18:27:46
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1640024866.95.0.391271511093.issue46051@roundup.psfhosted.org>
In-reply-to
Content
It cannot work this way.

atexit.register() as a function allows you to specify arguments which will be passed to the registered function, but if it is used as a decorator, only one argument (the function itself) is passed to atexit.register() (it is how decorators work). When used as a decorator it can only register functions without parameters.

On other hand, the function corresponding to a class method requires the class argument. There is no magic to determine it at the decoration time, because the class does not exist yet. It will be created only after the class definition body be executed.

It is possible to create a decorator which work the way you want. The simplest method perhaps to create a classmethod subclass with __set_name__() method. __set_name__() will be called after creating the class.

class mydecorator(classmethod):
    def __set_name__(self, type, name):
        atexit.register(self.__func__, type)

class Program:
    @mydecorator
    def clean_up(cls):
        ...
History
Date User Action Args
2021-12-20 18:27:46serhiy.storchakasetrecipients: + serhiy.storchaka, terry.reedy, quapka
2021-12-20 18:27:46serhiy.storchakasetmessageid: <1640024866.95.0.391271511093.issue46051@roundup.psfhosted.org>
2021-12-20 18:27:46serhiy.storchakalinkissue46051 messages
2021-12-20 18:27:46serhiy.storchakacreate