Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.freenet.ag!news2.euro.net!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'cache': 0.05; 'class,': 0.07; 'used.': 0.07; 'api': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:Help': 0.10; 'def': 0.10; 'instance:': 0.16; 'module:': 0.16; 'no-nonsense': 0.16; 'received:80.91.229.3': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-dialin.net': 0.16; 'save(self):': 0.16; 'subject:Singleton': 0.16; 'underscores': 0.16; 'wrote:': 0.17; 'config': 0.17; 'instance': 0.17; 'module,': 0.17; 'module': 0.19; 'import': 0.21; 'posts': 0.23; 'seems': 0.23; 'header:User-Agent:1': 0.26; 'wondering': 0.26; 'older': 0.27; 'module.': 0.27; 'object,': 0.27; 'options': 0.27; 'header:X -Complaints-To:1': 0.28; 'this?': 0.28; 'josh': 0.29; 'trigger': 0.29; 'value)': 0.29; 'skip:_ 10': 0.29; 'class': 0.29; "i'm": 0.29; "skip:' 10": 0.30; 'code': 0.31; 'skip:s 30': 0.33; 'handle': 0.33; 'to:addr:python-list': 0.33; 'there': 0.35; 'received:org': 0.36; 'but': 0.36; 'modules': 0.36; 'subject:with': 0.36; 'two': 0.37; 'subject:: ': 0.38; 'skip:o 20': 0.38; 'talk': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'your': 0.60; 'first': 0.61; 'different': 0.63; 'therefore': 0.65; 'configparser': 0.84; 'subject:skip:S 10': 0.84; 'incredibly': 0.96 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: Help with Singleton SafeConfigParser Date: Sat, 08 Dec 2012 18:40:07 +0100 Organization: None References: <113bded6-c75f-4322-9703-93420b4c3522@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8Bit X-Gmane-NNTP-Posting-Host: p5084934f.dip.t-dialin.net User-Agent: KNode/4.7.3 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: 56 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1354988369 news.xs4all.nl 6964 [2001:888:2000:d::a6]:45227 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:34494 Josh English wrote: > I have seen older posts in this group that talk about using modules as singletons, but this, unless I misunderstand, requires me to code the entire API for SafeConfigParser in the module: > >
> import ConfigParser
> 
> 
> class Options(ConfigParser.SafeConfigParser):
>     def __init__(self):
>         ConfigParser.SafeConfigParser.__init__(self)
>         self.readfp(open('defaults.cfg'))
>         self.read(['local.txt', 'local.cfg'])
> 
>     def save(self):
>         with open('local.txt','w') as f:
>             self.write(f)
> 
> __options = Options()
> 
> def set(section, name, value):
>     return self.__options.set(section, name, value)
> 
> def options(section):
>     return self.__options.options
> 
> # And so on
> 
> > This seems incredibly wasteful, and to introspect my options I get a module, not a SafeConfigParser object, so I'm wondering if there is a different way to handle this? Two underscores trigger name mangling only in a class, not in a module. Don't try to hide the Options instance: # module config.py import ConfigParser class Options(ConfigParser.SafeConfigParser): ... # as above options = Options() Then use it elsewhere: from config import options options.set("mysection", "myoption", "myvalue") All but the first import will find the module in the cache (sys.modules) and therefore the same Options instance will be used. VoilĂ  your no-nonsense singleton.