should "self" be changed?

Ian Kelly ian.g.kelly at gmail.com
Thu May 28 11:40:32 EDT 2015


On Thu, May 28, 2015 at 9:01 AM, Marko Rauhamaa <marko at pacujo.net> wrote:
> Anssi Saari <as at sci.fi>:
>
>> Do you have an example of state pattern using nested classes and
>> python? With a quick look I didn't happen to find one in any language.
>
> Here's an sampling from my mail server:

I think I would be more inclined to use enums. This has the advantages
of not creating a new set of state classes for every connection
instance and that each state is a singleton instance, allowing things
like "if self.state is SMTPConnectionState.IDLE". It could look
something like this:

class SMTPConnectionState(Enum):

    class IDLE:
        @classmethod
        def handle_command(cls, conn, cmd):
            # ...

    class SPF_HELO:
        @classmethod
        def terminate(cls, conn):
            # ...



More information about the Python-list mailing list