multi-instance and classic singleton design patterns

Ayose ayose.cazorla at hispalinux.es
Thu Aug 19 07:40:26 EDT 2004


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



More information about the Python-list mailing list