Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed3a.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.015 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'python,': 0.02; 'essentially': 0.04; '*is*': 0.09; 'way:': 0.09; 'python': 0.11; '"could': 0.16; 'constructor.': 0.16; 'from:addr:torriem': 0.16; 'from:name:michael torrie': 0.16; 'guessing': 0.16; 'singleton': 0.16; 'subject:?)': 0.16; 'top-level': 0.16; 'wrote:': 0.18; 'module': 0.19; 'meant': 0.20; 'thanks.': 0.20; 'import': 0.22; 'header:User-Agent:1': 0.23; 'config': 0.24; "haven't": 0.24; 'class.': 0.26; 'header:In-Reply-To:1': 0.27; 'feature': 0.29; "i'm": 0.30; 'code': 0.31; 'commonly': 0.31; 'maintains': 0.31; 'once,': 0.31; 'class': 0.32; 'run': 0.32; 'another': 0.32; 'implemented': 0.33; 'not.': 0.33; 'could': 0.34; 'problem': 0.35; 'subject: (': 0.35; 'created': 0.35; 'but': 0.35; 'module.': 0.36; 'should': 0.36; 'clear': 0.37; 'sometimes': 0.38; 'message- id:@gmail.com': 0.38; 'skip:[ 10': 0.38; 'to:addr:python-list': 0.38; 'fact': 0.38; 'pm,': 0.38; 'little': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'tell': 0.60; 'new': 0.61; 'first': 0.61; 'such': 0.63; 'more': 0.64; 'charset:windows-1252': 0.65; 'between': 0.67; 'sharing': 0.69; 'acts': 0.74; '(global': 0.84; 'state.': 0.95 X-Virus-Scanned: amavisd-new at torriefamily.org Date: Sat, 28 Feb 2015 22:05:16 -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: <13v4falb4odnhtvss4qdatnn16sgiv5pgd@4ax.com> 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: 25 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1425186322 news.xs4all.nl 2945 [2001:888:2000:d::a6]:39813 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:86645 On 02/28/2015 09:29 PM, yves@zioup.com wrote: >> Problem 4: >> You speak of a singleton. But you haven't implemented one. It is not >> clear from your code if this class should be a singleton. I'm guessing >> not. Singletons are in fact rare. Well, let me put it another way: >> Good reasons to code a singleton are rare. > > Thanks. I hadn't realise "singleton" meant a class built such that it could > not be instanciated more than once, I thought it corresponded to a pattern > where only one object is ever created from a given class. Is not "only one object is ever created from a given class" the same as "could not be [instantiated] more than once?" To be clear, singletons are commonly used in Python every time you import a module. A module *is* a singleton pattern, particularly one that maintains state. I use sometimes use this feature for sharing config and other data between other modules (global state when it's required). "import module" essentially does the one-time instantiation if it's a new import; top-level code in the module is run once on first import, where it acts like a constructor. So it functions as a singleton. As near as I can tell there's very little reason to make a class-based singleton in Python, since a module has the same effect and is cleaner.