Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!1.eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.016 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; '"if': 0.09; 'server:': 0.09; 'def': 0.14; 'thu,': 0.15; '@classmethod': 0.16; 'conn):': 0.16; 'conn,': 0.16; 'inclined': 0.16; 'singleton': 0.16; 'wrote:': 0.16; 'instance,': 0.18; 'python?': 0.18; 'nested': 0.22; 'am,': 0.23; '2015': 0.23; 'this:': 0.23; 'header:In-Reply- To:1': 0.24; 'example': 0.25; 'message-id:@mail.gmail.com': 0.28; 'classes': 0.30; 'connection': 0.30; 'creating': 0.32; 'language.': 0.32; 'class': 0.33; 'subject:?': 0.34; 'received:google.com': 0.34; 'could': 0.35; 'to:addr:python-list': 0.35; 'instance': 0.35; 'something': 0.35; 'subject:" ': 0.36; "didn't": 0.37; 'subject:: ': 0.37; 'things': 0.39; 'to:addr:python.org': 0.39; 'more': 0.62; 'advantages': 0.72; 'subject:self': 0.84; 'to:name:python': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=0olfGmeWrrYJ/3DdaI29dlmSemq0v+ZC1d9F9z/IKew=; b=FXdYtB54bBk9k0xv2JS4GooP0kFXAn92JGPW3xhxBeGtwHiiruT7XhS3Pmyf/TJosj ckdF39O1FYa6cm+HSm+J9NtpWcTCwOYdxo8ZQH8QEWQbiJlA2MehjF9fups2OUEF5InM 40R/wuihvBNAs3ccA0ndvC6tGGqvsMj0mgaSYrOkKTssv49XbGEYXRgsN0k4qy9MIqUD 2nZAXhUAcGsafbyqGAlE89nwmGVeZ+zeqDSdEZEymMOFBJDUB7ogtxodKPDNe2bnHZaK eZc+QG74FY26TbHfA5HKctpN5seD80jKmNWuBnrT4O9uNvaF6N1nfrjJLF68RRW7KV5i Qxyw== X-Received: by 10.107.6.136 with SMTP id f8mr4110537ioi.61.1432827673096; Thu, 28 May 2015 08:41:13 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: <87siagagpx.fsf@elektro.pacujo.net> References: <551c8229-f426-45f0-a0ee-fdad1b161f59@googlegroups.com> <877frvf7f2.fsf@elektro.pacujo.net> <9f9498a5-8291-4347-aef2-ada324bb47a7@googlegroups.com> <87r3q3dqju.fsf@elektro.pacujo.net> <87siagagpx.fsf@elektro.pacujo.net> From: Ian Kelly Date: Thu, 28 May 2015 09:40:32 -0600 Subject: Re: should "self" be changed? To: Python Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 25 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1432827676 news.xs4all.nl 2951 [2001:888:2000:d::a6]:52347 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:91376 On Thu, May 28, 2015 at 9:01 AM, Marko Rauhamaa wrote: > Anssi Saari : > >> 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): # ...