Catalog of Python Patterns? [LONG!]

Andy Gimblett gimbo at ftech.net
Fri Apr 5 03:51:16 EST 2002


On Thu, Apr 04, 2002 at 05:47:32PM -0800, Geoff Gerrietts wrote:

> I think that you could make a case for the limited applicability of
> Command pattern, the Singleton pattern, the Prototype pattern, and I'm
> sure several others. You could implement them, but there's not a solid
> reason to do so, because Python doesn't impose the restrictions that
> the patterns are designed to circumvent.

Really?  I've used Singleton a fair bit (probably too much in fact)
for stuff like configuration information.  I'm curious as to how else
I could/should do this.

Here's how I'd do it now:

-- begin Config.py -- 

_configInstance = None

def Config(args=None):
    global _configInstance
    if _configInstance is None:
        _configInstance = ConfigInstance(args)
    return _configInstance

class ConfigInstance:
    def __init__(self, args=None):
	# do some stuff

-- end Config.py --

Then to use it elsewhere I just do:

import Config
config = Config.Config()
if config.send_error_emails:
    send_it()

etc.

and I know what I'm getting.

If you can suggest a better/cleaner/simpler/more pythonic way of
achieving this, I'd be honestly interested.  :-)

Cheers,

Andy

-- 
Andy Gimblett - Programmer - Frontier Internet Services Limited
Tel: 029 20 820 044 Fax: 029 20 820 035 http://www.frontier.net.uk/
Statements made are at all times subject to Frontier's Terms and
Conditions of Business, which are available upon request.





More information about the Python-list mailing list