Keeping track of subclasses and instances?

Michele Simionato michele.simionato at gmail.com
Thu Oct 11 04:19:59 EDT 2007


On Oct 11, 7:16 am, Steven D'Aprano <st... at REMOVE-THIS-
cybersource.com.au> wrote:
> Is there a problem with writing C like this?
>
> class C(object):
>     def __init__(self):
>         logging.warn('Allocating resource ...')
>         self.__log = logging.warn
>     def __del__(self):
>         self.__log('De-allocating resource ...')
>         print 'THIS IS REACHED!'
>
> It works for me. Have I missed something?

Another more common workaround is to use default arguments:

class C(object):
    def __init__(self):
        logging.warn('Allocating resource ...')
    def __del__(self, warn=logging.warn):
        warn('De-allocating resource ...')
        print 'THIS IS REACHED!'

But, as you know, I want to remove __del__ because of the problem with
reference
cycles, not because of this little issue with the cleanup mechanism
(which BTW is shared
also by the weak references callback mechanism).

        Michele Simionato




More information about the Python-list mailing list