singleton (newbie)

Nicolas Évrard nicoe at no-log.org
Thu Aug 19 03:31:37 CEST 2004


* 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