Design thought for callbacks

Marko Rauhamaa marko at pacujo.net
Sun Feb 22 08:54:04 EST 2015


Cem Karan <cfkaran2 at gmail.com>:

> You were saying that you move your objects into a zombie state.  I
> assumed that you meant you marked them in some manner (e.g., setting
> 'is_zombie' to True),

Yes, but even better:

    self.set_state(ZOMBIE)

>  so that anything that has a strong reference to the object knows the
>  object is not supposed to be used anymore.

The other way round: the zombie object knows to ignore callbacks sent
its way. It's not the responsibility of the sender to mind the
receiver's internal state.

I nowadays tend to implement states as inner classes. Here's how I've
implemented the zombie state of one class:

class Delivery...:
    def __init__(...):
        ...
        class ZOMBIE(STATE):
            def handle_connected(self):
                pass
            def handle_eof(self):
                pass
            def handle_response(self, code, response):
                pass
            def handle_io_error(self, errcode):
                pass
            def zombifie(self):
                assert False
            def transaction_timeout(self):
                assert False


Marko



More information about the Python-list mailing list