Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

multi-instance and classic singleton design patterns

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


nzanella at cs

Aug 18, 2004, 2:54 PM

Post #1 of 4 (351 views)
Permalink
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();
}
--
http://mail.python.org/mailman/listinfo/python-list


ptmcg at austin

Aug 18, 2004, 3:17 PM

Post #2 of 4 (316 views)
Permalink
Re: multi-instance and classic singleton design patterns [In reply to]

"Neil Zanella" <nzanella [at] cs> wrote in message
news:b68d2f19.0408181354.43821bd9 [at] posting
> 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/Cookbook/Python/Recipe/66531 .

-- Paul

(You can also try googling for "python singleton borg" to find this and many
other helpful suggestions and comments.)


--
http://mail.python.org/mailman/listinfo/python-list


raims at dot

Aug 18, 2004, 3:22 PM

Post #3 of 4 (321 views)
Permalink
Re: multi-instance and classic singleton design patterns [In reply to]

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/Cookbook/Python/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
--
http://mail.python.org/mailman/listinfo/python-list


ayose.cazorla at hispalinux

Aug 19, 2004, 4:40 AM

Post #4 of 4 (334 views)
Permalink
Re: multi-instance and classic singleton design patterns [In reply to]

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
--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.