Path: csiph.com!usenet.pasdenom.info!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python,': 0.02; 'function,': 0.09; 'instance.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'class:': 0.16; 'factory': 0.16; 'finney': 0.16; 'it;': 0.16; 'namespace,': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'roy': 0.16; 'sense,': 0.16; 'singleton': 0.16; ':-)': 0.16; 'language': 0.16; 'wrote:': 0.18; 'module': 0.19; 'trying': 0.19; '>>>': 0.22; 'example': 0.22; 'creating': 0.23; 'header:User-Agent:1': 0.23; 'received:comcast.net': 0.24; 'looks': 0.24; "i've": 0.25; 'header:X-Complaints-To:1': 0.27; 'van': 0.27; 'header:In-Reply- To:1': 0.27; 'point': 0.28; 'am,': 0.29; 'needed.': 0.30; '>>>>': 0.31; 'convince': 0.31; 'object.': 0.31; 'them?': 0.31; 'writes:': 0.31; 'class': 0.32; 'sense': 0.34; 'but': 0.35; 'there': 0.35; 'really': 0.36; "he's": 0.36; 'instances': 0.36; 'next': 0.36; 'two': 0.37; 'implement': 0.38; 'ben': 0.38; 'to:addr:python- list': 0.38; 'explain': 0.39; 'does': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'more': 0.64; 'different': 0.65; 'skip:\xe2 10': 0.65; 'effectively': 0.66; '8bit%:40': 0.68; 'smith': 0.68; '4:00': 0.84; "class's": 0.84; 'virtue': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Ned Batchelder Subject: Re: singleton ... again Date: Thu, 13 Feb 2014 06:50:36 -0500 References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Gmane-NNTP-Posting-Host: c-50-133-228-126.hsd1.ma.comcast.net User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:24.0) Gecko/20100101 Thunderbird/24.3.0 In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 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: 37 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1392292254 news.xs4all.nl 2837 [2001:888:2000:d::a6]:46841 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:66166 On 2/13/14 4:00 AM, Piet van Oostrum wrote: > Ben Finney writes: > >> Gregory Ewing writes: >> >>> Roy Smith wrote: >>>> It looks to me like he's trying to implement a classic Gang of Four >>>> singleton pattern. >>> >>> Which I've never really seen the point of in Python, or any other >>> language for that matter. Just create one instance of the class during >>> initialisation, put it in a global somewhere, and use it thereafter. >> >> Make that “somewhere” a module namespace, and you effectively have a >> Singleton for all practical purposes. So yes, I see the point of it; but >> we already have it built in :-) > > There is a use case for a singleton class: when creating the singleton > object takes considerable resources and you don't need it always in your > program. > I still don't see it. To convince me that a singleton class makes sense, you'd have to explain why by virtue of the class's very nature, it never makes sense for there ever to be more than one of them. Your example is an expensive-to-create object. Why does that mean I might not want two of them? I can see how it makes sense to have a factory function, which will make one only when asked, and will hold onto that object for the next time it's needed. But that's different than a class which pretends to make instances but actually always returns the same instance. -- Ned Batchelder, http://nedbatchelder.com