468,783 Members | 1,572 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,783 developers. It's quick & easy.

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 2150
"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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

37 posts views Thread by ajikoe | last post: by
4 posts views Thread by Frank Jona | last post: by
17 posts views Thread by =?Utf-8?B?R2Vvcmdl?= | last post: by
1 post views Thread by CARIGAR | last post: by
2 posts views Thread by Marin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.