Singleton in Python Cookbook

  • Thread starter Bruno Desthuilliers
  • Start date
B

Bruno Desthuilliers

Alex Popescu a écrit :
Hi all!

I was reading through Python Cookbook the Singleton recipe. At this moment
I am a bit puzzled as the example in the book is not working resulting in:

TypeError: type.__new__(SingleSpam): SingleSpam is not a subtype of type

(I haven't presented the original code as I am not sure about copyrights).

I don't have the book, so if you don't post the code, I just give up
trying to guess what the problem can be.
 
A

Alex Popescu

Hi all!

I was reading through Python Cookbook the Singleton recipe. At this moment
I am a bit puzzled as the example in the book is not working resulting in:

TypeError: type.__new__(SingleSpam): SingleSpam is not a subtype of type

(I haven't presented the original code as I am not sure about copyrights).

bests,
../alex
 
D

Diez B. Roggisch

Alex said:
Hi all!

I was reading through Python Cookbook the Singleton recipe. At this moment
I am a bit puzzled as the example in the book is not working resulting in:

TypeError: type.__new__(SingleSpam): SingleSpam is not a subtype of type

(I haven't presented the original code as I am not sure about copyrights).

AFAIK the cookbook is completely found online at ASPN. So no sweat
publishing it here.


And regarding the problem: is it possible that you forgot to subclass
SingleSpam from object?

Diez
 
A

Alex Popescu

AFAIK the cookbook is completely found online at ASPN. So no sweat
publishing it here.


And regarding the problem: is it possible that you forgot to subclass
SingleSpam from object?

Diez

The exact code:
class Singleton(object):
""" A Pythonic Singleton """
def _ _new_ _(cls, *args, **kwargs):
if '_inst' not in vars(cls):
cls._inst = type._ _new_ _(cls, *args, **kwargs)
return cls._inst

if _ _name_ _ == '_ _main_ _':
class SingleSpam(Singleton):
def _ _init_ _(self, s): self.s = s
def _ _str_ _(self): return self.s
s1 = SingleSpam('spam')
print id(s1), s1.spam( )
s2 = SingleSpam('eggs')
print id(s2), s2.spam( )

../alex
 
A

Alex Popescu

The exact code:
class Singleton(object):
""" A Pythonic Singleton """
def _ _new_ _(cls, *args, **kwargs):
if '_inst' not in vars(cls):
cls._inst = type._ _new_ _(cls, *args, **kwargs)
return cls._inst

if _ _name_ _ == '_ _main_ _':
class SingleSpam(Singleton):
def _ _init_ _(self, s): self.s = s
def _ _str_ _(self): return self.s
s1 = SingleSpam('spam')
print id(s1), s1.spam( )
s2 = SingleSpam('eggs')
print id(s2), s2.spam( )

./alex
I got it working in 2 ways:

class Singleton(object):
""" A Pythonic Singleton """
def __new__(cls, *args, **kwargs):
if '_singletoninstance' not in vars(cls):
#variant 1: cls._singletoninstance = object.__new__(cls, *args,
**kwargs)
#variant 2: cls._singletoninstance = super(type, cls).__new__(cls,
*args, **kwargs)
return cls._singletoninstance

Both of these seem to work.

../alex
 
J

John J. Lee

Diez B. Roggisch said:
AFAIK the cookbook is completely found online at ASPN. So no sweat
publishing it here.
[...]

No: the book-form cookbook is edited, and has extra text.

I believe the recipes are under a BSD-style license, though.


John
 
S

Steve Holden

Alex said:
I got it working in 2 ways:

class Singleton(object):
""" A Pythonic Singleton """
def __new__(cls, *args, **kwargs):
if '_singletoninstance' not in vars(cls):
#variant 1: cls._singletoninstance = object.__new__(cls, *args,
**kwargs)
#variant 2: cls._singletoninstance = super(type, cls).__new__(cls,
*args, **kwargs)
return cls._singletoninstance

Both of these seem to work.
If, that is, "work" means "Raise an AttributeError due to the missing
spam() method". This appears to fix that problem:

class Singleton(object):
""" A Pythonic Singleton """
def __new__(cls, *args, **kwargs):
if '_inst' not in vars(cls):
cls._inst = object.__new__(cls, *args, **kwargs)
return cls._inst

if __name__ == '__main__':
class SingleSpam(Singleton):
def __init__(self, s): self.s = s
def __str__(self): return self.s
s1 = SingleSpam('spam')
print id(s1), s1
s2 = SingleSpam('eggs')
print id(s2), s2
print str(s1)

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
--------------- Asciimercial ------------------
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
----------- Thank You for Reading -------------
 
A

Alex Popescu

Alex Popescu a écrit :
[snip...]


I don't have the book, so if you don't post the code, I just give up
trying to guess what the problem can be.

I've sent the original code and 2 different variants a long time ago.

../alex
 
B

Bruno Desthuilliers

Alex Popescu a écrit :
Alex Popescu a écrit :
[snip...]


I don't have the book, so if you don't post the code, I just give up
trying to guess what the problem can be.

I've sent the original code and 2 different variants a long time ago.

You had not sent anything when I answered. But it seems that the machine
I answered with is a bit out of time !-)
 

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,774
Messages
2,569,596
Members
45,143
Latest member
SterlingLa
Top