_Re: singleton (newbie)

N

Neil Zanella

Hello,

Is this the correct way to code multi-instance singleton in Python?
It seems to do the trick for me but I appreciate any criticism as I
am somewhat new to Python.

Thanks,

Neil

#!/usr/bin/python

class B:
x = 0
y = 1
def foo(): print B.x
foo = staticmethod(foo)
def bar(): print B.y; B.y += 1
bar = staticmethod(bar)

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

Larry Bates

Neil,

Three observations:

1) IMHO Python needs to be written as Python not as some
other language. I've rewritten your class below:

class B:
x = 0
y = 1

def foo():
print B.x

foo = staticmethod(foo)

def bar():
print B.y
B.y+=1

bar = staticmethod(bar)

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

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()

if you want x, y to be local to current instance of B:

class B:
def __init__(self):
self.x=0
self.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()

3) If you just want static functions, just write them that way.

def foo():
global x
print x

def bar():
global y
print y

if __name__ == "__main__":
x=0
y=0
foo()
bar()
foo()
bar()
bar()

Maybe it will help, maybe not.

Larry Bates
Syscon, Inc.
 
N

Nicolas =?iso-8859-15?Q?=C9vrard?=

* 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.
 
L

Larry Bates

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 said:
* 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,114
Latest member
GlucoPremiumReview
Top