Path: csiph.com!usenet.pasdenom.info!gegeweb.org!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed2.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.025 X-Spam-Evidence: '*H*': 0.95; '*S*': 0.00; 'example:': 0.03; 'debugging': 0.07; 'already.': 0.09; 'attributes': 0.09; 'scripts,': 0.09; 'url:github': 0.09; 'from:addr:torriem': 0.16; 'from:name:michael torrie': 0.16; 'repl': 0.16; 'singleton': 0.16; 'subject:?)': 0.16; 'url:py': 0.16; 'wrote:': 0.18; 'module': 0.19; 'trying': 0.19; 'code,': 0.22; 'this?': 0.23; 'header:User- Agent:1': 0.23; 'class.': 0.26; 'header:In-Reply-To:1': 0.27; 'appear': 0.29; 'especially': 0.30; 'code': 0.31; 'though.': 0.31; 'class': 0.32; 'interface': 0.32; 'skip:d 20': 0.34; 'subject: (': 0.35; 'good.': 0.35; 'objects': 0.35; 'but': 0.35; 'there': 0.35; 'really': 0.36; 'should': 0.36; 'example,': 0.37; 'implement': 0.38; 'message-id:@gmail.com': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'does': 0.39; 'functional': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'read': 0.60; 'address': 0.63; 'kind': 0.63; 'places': 0.64; 'more': 0.64; 'charset:windows-1252': 0.65; 'potentially': 0.81; 'clearer': 0.84; 'url:master': 0.84; 'difficult,': 0.91; 'state.': 0.95 X-Virus-Scanned: amavisd-new at torriefamily.org Date: Sat, 28 Feb 2015 19:19:32 -0700 From: Michael Torrie User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.5.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: suggestions for functional style (singleton pattern?) References: In-Reply-To: Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.19 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: 28 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1425176390 news.xs4all.nl 2905 [2001:888:2000:d::a6]:48463 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:86640 On 02/28/2015 04:12 PM, yves@zioup.com wrote: > For some scripts, I write in a a more functional way, using a lot of small > functions outside of any class. Although it makes the code clearer for > specific cases, I have found that it makes debugging and using the repl in > general difficult, as as I have to re-initialise every single objects every time. > > I have now started to use some kind of state pattern to alleviate this, here's > a simplistic example: > > https://github.com/dorfsmay/state_pattern_for_debugging_python/blob/master/dirstats.py > > Are there better ways to address this? Any suggestion on this style? You say you are trying to use a singleton pattern, but your code does not appear to implement a singleton. From what I can read of your code, you really should just put all your functions as methods to the DirStat class and call it good. Especially if your module is going to be used in several places potentially at the same time. If what you want is a singleton, then that's what a module is already. Module functions are singleton methods and module attributes maintain state. Just call your module dirstat and interface with it: dirstat.opendir(**vars(args) dirstat.print() In this example, I really don't think the singleton is a good pattern, though.