singleton (newbie)

Larry Bates lbates at swamisoft.com
Thu Aug 19 15:53:24 CEST 2004


Seems you are correct.  I should have written:

class B:
    x = 0
    y = 1

    def foo(self):
        print self.__class__.x

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

if __name__ == "__main__":
    b=B()
    b.foo()
    b.bar()
    b.foo()
    b.bar()
    b.bar()
    a=B()
    a.foo()
    a.bar()
    a.bar()


Thanks for pointing this out.

-Larry

"Nicolas Évrard" <nicoe at no-log.org> wrote in message
news:mailman.1915.1092879132.5135.python-list at python.org...
> * Larry Bates  [01:54 19/08/04 CEST]:
> >2) I continue to read on c.l.p. about staticmethods and
> >   just don't understand why anyone would use them.  This
> >   is how I learned to write this in Python.  It seems that
> >   they are some sort of "carryover" from another language.
> >   I'd be the first to admit I don't understand the appeal,
> >   so maybe they can be useful.  I've just never needed them.
> >   If I need a static function, I just write it that way.
> >   I don't make it the method of a class object.
> >
> >if you want x, y to be global across all instances of B:
> >
> >class B:
> >    x = 0
> >    y = 1
> >
> >    def foo(self):
> >        print self.x
> >
> >    def bar(self):
> >        print self.y
> >        self.y+=1
> >
> >if __name__ == "__main__":
> >    b=B()
> >    b.foo()
> >    b.bar()
> >    b.foo()
> >    b.bar()
> >    b.bar()
>
> It won't work. Different instances of B will have different values for
> y.
>
> -- 
> (°>  Nicolas Évrard
> / )  Liège - Belgique
> ^^





More information about the Python-list mailing list