Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #34502

Re: Help with Singleton SafeConfigParser

From Mark Lawrence <breamoreboy@yahoo.co.uk>
Subject Re: Help with Singleton SafeConfigParser
Date 2012-12-08 20:23 +0000
References <113bded6-c75f-4322-9703-93420b4c3522@googlegroups.com> <mailman.625.1354988369.29569.python-list@python.org> <e33d91bc-cb8f-4414-9f8c-1339cecfc6f5@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.630.1354998232.29569.python-list@python.org> (permalink)

Show all headers | View raw


On 08/12/2012 17:48, Josh English wrote:
> On Saturday, December 8, 2012 9:40:07 AM UTC-8, Peter Otten wrote:
>>
>> 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.
>
>
> Ah. I was over-thinking again. I couldn't find an example of this anywhere, and when I saw the tirades against Singletons they mentioned "use modules" but, well, I haven't had my morning coffee yet. I shouldn't even be trying this sort of thing until then.
>
> Thank you for the simple answer.
>
> Josh
>

For the benefit of the OP and others, if you want to gain more knowledge 
about patterns in Python such as the Singleton, I suggest you use your 
favourite search engine to find "Alex Martelli Python patterns".

-- 
Cheers.

Mark Lawrence.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Help with Singleton SafeConfigParser Josh English <Joshua.R.English@gmail.com> - 2012-12-08 09:11 -0800
  Re: Help with Singleton SafeConfigParser Peter Otten <__peter__@web.de> - 2012-12-08 18:40 +0100
    Re: Help with Singleton SafeConfigParser Josh English <Joshua.R.English@gmail.com> - 2012-12-08 09:48 -0800
      Re: Help with Singleton SafeConfigParser Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-12-08 20:23 +0000
    Re: Help with Singleton SafeConfigParser Josh English <Joshua.R.English@gmail.com> - 2012-12-08 09:48 -0800

csiph-web