473,503 Members | 12,103 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

multi-instance and classic singleton design patterns

Hello,

I would be very interested in knowing how the following C++ multi-instance
singleton (AKA Borg) design pattern based code snippet can be neatly coded
in Python. While there may be somewhat unusual places where multi-instance
singleton is more useful than plain singleton, it seems to me that the
former leads to less coding, so unless I can somehow package the
singleton pattern in a superclass (so I don't have to code it
explicityly in every singleton class I have), then I am more
interested in the multi-instance singleton design pattern.

Thanks,

Neil

#include <iostream>

class B {
public:
B() { }
void foo() const {
std::cout << x << std::endl;
}
void bar() {
std::cout << y++ << std::endl;
}
private:
static const int x = 0;
static int y;
};

int B::y = 1;

int main() {
B().foo();
B().bar();
B().foo();
B().bar();
B().bar();
}
Jul 18 '05 #1
4 2401
"Neil Zanella" <nz******@cs.mun.ca> wrote in message
news:b6**************************@posting.google.c om...
Hello,

I would be very interested in knowing how the following C++ multi-instance
singleton (AKA Borg) design pattern based code snippet can be neatly coded
in Python. While there may be somewhat unusual places where multi-instance
singleton is more useful than plain singleton, it seems to me that the
former leads to less coding, so unless I can somehow package the
singleton pattern in a superclass (so I don't have to code it
explicityly in every singleton class I have), then I am more
interested in the multi-instance singleton design pattern.

Thanks,

Neil

<snip C++ example code>

Neil -

You may be new to the Python community, given that you are porting a C++
pattern to Python. There is a terrific resource for snippets of this kind
in the Python Cookbook, to be found at
http://aspn.activestate.com/ASPN/Cookbook/Python/ .

You should find a *very* neat implementation of this pattern at the bottom
of http://aspn.activestate.com/ASPN/Coo...n/Recipe/66531 .

-- Paul

(You can also try googling for "python singleton borg" to find this and many
other helpful suggestions and comments.)
Jul 18 '05 #2
In data 18 Aug 2004 14:54:05 -0700, Neil Zanella ha scritto:
it seems to me that the
former leads to less coding, so unless I can somehow package the
singleton pattern in a superclass (so I don't have to code it
explicityly in every singleton class I have), then I am more
interested in the multi-instance singleton design pattern.


Seems that Alex Martelli coded a Borg implementation in Python
sometimes ago:
http://aspn.activestate.com/ASPN/Coo...n/Recipe/66531

--
Lawrence (l dot oluyede at virgilio dot it)
"If the implementation is hard to explain, it's a bad idea."
from The Zen of Python by Tim Peters
Jul 18 '05 #3
On Wed, Aug 18, 2004 at 02:54:05PM -0700, Neil Zanella wrote:

#include <iostream>

class B {
public:
B() { }
void foo() const {
std::cout << x << std::endl;
}
void bar() {
std::cout << y++ << std::endl;
}
private:
static const int x = 0;
static int y;
};

int B::y = 1;

int main() {
B().foo();
B().bar();
B().foo();
B().bar();
B().bar();
}


Try with

class B:
x = 0

def foo(self):
print B.x

def bar(self):
B.y += 1
print B.y
B.y = 1

if __name__ == '__main__':
B().foo()
B().bar()
B().foo()
B().bar()
B().bar()
--
Ayose Cazorla León
Debian GNU/Linux - setepo
Jul 18 '05 #4

I need to take an email with none or more attachments, remove attachments
with certain file extensions and then send the remaining email onwards.

The problem is that I can't find out how to remove single attachements, (I
can add new ones and/or remove all) .

Am I missing something, or do I have to create a new email object from the
remaining bits of the old one ?

(I *have* googled)

TIA
Jul 18 '05 #5

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

Similar topics

37
4832
by: ajikoe | last post by:
Hello, Is anyone has experiance in running python code to run multi thread parallel in multi processor. Is it possible ? Can python manage which cpu shoud do every thread? Sincerely Yours,...
4
4630
by: Frank Jona | last post by:
Intellisense with C# and a multi-file assembly is not working. With VB.NET it is working. Is there a fix availible? We're using VisualStudio 2003 Regards Frank
12
3842
by: * ProteanThread * | last post by:
but depends upon the clique: ...
0
3746
by: frankenberry | last post by:
I have multi-page tiff files. I need to extract individual frames from the multi-page tiffs and save them as single-page tiffs. 95% of the time I receive multi-page tiffs containing 1 or more black...
6
4870
by: Joe | last post by:
I have 2 multi-list boxes, 1 displays course categories based on a table called CATEGORIES. This table has 2 fields CATEGORY_ID, CATEGORY_NAME The other multi-list box displays courses based on...
4
17821
by: mimmo | last post by:
Hi! I should convert the accented letters of a string in the correspondent letters not accented. But when I compile with -Wall it give me: warning: multi-character character constant Do the...
5
5962
by: Shane Story | last post by:
I can seem to get the dimensions of a frame in a multiframe tiff. After selecting activeframe, the Width/Height is still really much larger than the page's actual dimensions. When I split a...
5
3236
by: dkelly925 | last post by:
Is there a way to add an If Statement to the following code so if data in a field equals "x" it will launch one report and if it equals "y" it would open another report. Anyone know how to modify...
17
10613
by: =?Utf-8?B?R2Vvcmdl?= | last post by:
Hello everyone, Wide character and multi-byte character are two popular encoding schemes on Windows. And wide character is using unicode encoding scheme. But each time I feel confused when...
1
9284
by: mknoll217 | last post by:
I am recieving this error from my code: The multi-part identifier "PAR.UniqueID" could not be bound. The multi-part identifier "Salary.UniqueID" could not be bound. The multi-part identifier...
0
7098
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7296
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
7364
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7017
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7470
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5604
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,...
1
5026
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...
0
3186
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
405
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.