473,399 Members | 2,278 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Re: Help with Borg design Pattern

Le Saturday 28 June 2008 03:47:43 Casey McGinty, vous avez écrit*:
On Fri, Jun 27, 2008 at 3:21 PM, Casey McGinty <ca***********@gmail.com>

wrote:
Hi,

I'm trying to implement a simple Borg or Singleton pattern for a class
that inherits from 'dict'. Can someone point out why this code does not
work?

class MyDict( dict ):
__state = {}
def __init__(self):
self.__dict__ = self.__state

a = MyDict()
a['one'] = 1
a['two'] = 2

print a
print MyDict()
Well, it works !
>>>[156]: class MyDict( dict ):
__state = {}
def __init__(self):
self.__dict__ = self.__state
.....:
.....:
>>>[160]: MyDict().toto = 5
>>>[161]: MyDict().toto
...[161]: 5

but the __dict__ attribute is the container of attributes of an instance,
which are not used in the underlying implementation of dictinnaries.
This looks like a good solution:

class MyDict( dict ):
def __new__(cls,*p,**k):
if not '_instance' in cls.__dict__:
cls._instance = dict.__new__(cls)
return cls._instance
Yes it is, but it's rather unneeded in Python, we prefer simply create a
module level dictionnary, these tricks are used in language like C++ or Java.

In python :

mymodule.py :

ModuleOptions = {}

othermodule.py :

import mymodule

mymodule.ModuleOptions['Verbose'] = True

or if you think encapsulation is important :

mymodule.py :

_ModuleOptions = {}

def get_option(opt) :
return _ModuleOptions[opt]
....

And you're done.

--
_____________

Maric Michaud
Jun 28 '08 #1
0 826

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Mars | last post by:
I have looked long and hard at Mr. Martelli's Borg recipe: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66531 It is a very useful substitute for a Singleton, but I can't figure out...
26
by: Uwe Mayer | last post by:
Hi, I've been looking into ways of creating singleton objects. With Python2.3 I usually used a module-level variable and a factory function to implement singleton objects. With Python2.4 I...
11
by: FluffyCat | last post by:
In Febraury - April of 2002 I put together in Java examples of all 23 of the classic "Gang Of Four" design patterns for my website. Partly I wanted to get a better understanding of those patterns....
1
by: Nick | last post by:
I have read a few months ago about a policy design pattern but can find nothing on it now. Does this design pattern exist and if so could someone explain what it is or links to a description. ...
22
by: Krivenok Dmitry | last post by:
Hello All! I am trying to implement my own Design Patterns Library. I have read the following documentation about Observer Pattern: 1) Design Patterns by GoF Classic description of Observer....
2
by: Jobs | last post by:
Download the JAVA , .NET and SQL Server interview with answers Download the JAVA , .NET and SQL Server interview sheet and rate yourself. This will help you judge yourself are you really worth of...
6
by: perhapscwk | last post by:
.... ... ... Both of the procedures have repeating sections of code: sending an e-mail and adding a log entry. Task: *1.Select a design pattern (substantiate your choice) that would allow...
5
by: Lie | last post by:
This is probably unrelated to Python, as this is more about design pattern. I'm asking your comments about this design pattern that is similar in functionality to Singleton and Borg: to share...
5
matheussousuke
by: matheussousuke | last post by:
Hello, I'm using tiny MCE plugin on my oscommerce and it is inserting my website URL when I use insert image function in the emails. The goal is: Make it send the email with the URL...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.