473,785 Members | 2,369 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 2422
"Neil Zanella" <nz******@cs.mu n.ca> wrote in message
news:b6******** *************** ***@posting.goo gle.com...
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
4899
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, Pujo
4
4676
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
3880
by: * ProteanThread * | last post by:
but depends upon the clique: http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&threadm=954drf%24oca%241%40agate.berkeley.edu&rnum=2&prev=/groups%3Fq%3D%2522cross%2Bposting%2Bversus%2Bmulti%2Bposting%2522%26ie%3DUTF-8%26oe%3DUTF-8%26hl%3Den ...
0
3789
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 and white CCITT4 compressed files (frames) inside the tiff. Every now and then I receive a mixture of black and white CCITT4 and JPEG compressed files, and sometimes just multi-page tiffs with JPEG only. The code runs great when dealing with the...
6
4897
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 a table called COURSES. This table has 2 fields CATEGORY_ID, COURSE_NAME. The CATEGORY_ID is a FK in COURSES and a PK in CATEGORIES. I want to populate the course list box based on any category(s)
4
17878
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 problem is the charset? How I can avoid this warning? But the worst thing isn't the warning, but that the program doesn't work! The program execute all other operations well, but it don't print the converted letters: for example, in the string...
5
6000
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 TIFF to several PNG files this causes a problem, becuase the resulting image is (the page to the far left and a lot of black space surrounding it and a filesize that is larger than needed. Any ideas?
5
3287
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 this? Private Sub cmdPreview_Click() On Error GoTo Err_Handler 'Purpose: Open the report filtered to the items selected in the list box. 'Author: Allen J Browne, 2004. http://allenbrowne.com Dim varItem As Variant 'Selected items
17
10709
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 talking with another team -- codepage -- at the same time. I am more confused when I saw sometimes we need codepage parameter for wide character conversion, and sometimes we do not need for conversion. Here are two examples,
1
9319
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 "PAR.UniqueID" could not be bound. The multi-part identifier "PAR.PAR_Status" could not be bound. The multi-part identifier "Salary.New_Salary" could not be bound. The multi-part identifier "Salary.UniqueID" could not be bound. The multi-part...
0
9645
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9481
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10155
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10095
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9954
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7502
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5513
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4054
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3656
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.