471,354 Members | 2,129 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,354 software developers and data experts.

Singleton in Python Cookbook

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
--
..w( the_mindstorm )p.

Jul 25 '07 #1
9 1914
Alex Popescu schrieb:
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
Jul 25 '07 #2
"Diez B. Roggisch" <de***@nospam.web.dewrote in
news:5g*************@mid.uni-berlin.de:
Alex Popescu schrieb:
>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
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
--
..w( the_mindstorm )p.

Jul 25 '07 #3
Alex Popescu <no*****************@gmail.comwrote in
news:Xn**************************@80.91.229.5:
"Diez B. Roggisch" <de***@nospam.web.dewrote in
news:5g*************@mid.uni-berlin.de:
>Alex Popescu schrieb:
>>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

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
--
.w( the_mindstorm )p.
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
--
..w( the_mindstorm )p.

Jul 25 '07 #4
"Diez B. Roggisch" <de***@nospam.web.dewrites:
[...]
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
Jul 25 '07 #5
Alex Popescu <no*****************@gmail.comwrites:
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
Haven't looked at that recipe, but take a look at this related one:

http://aspn.activestate.com/ASPN/Coo...n/Recipe/66531

John
Jul 25 '07 #6
Alex Popescu wrote:
Alex Popescu <no*****************@gmail.comwrote in
news:Xn**************************@80.91.229.5:
>"Diez B. Roggisch" <de***@nospam.web.dewrote in
news:5g*************@mid.uni-berlin.de:
>>Alex Popescu schrieb:
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
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
--
.w( the_mindstorm )p.
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 -------------

Jul 26 '07 #7
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.
Jul 26 '07 #8
Bruno Desthuilliers <bd*****************@free.quelquepart.frwrote in
news:46***********************@news.free.fr:
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
--
..w( the_mindstorm )p.
Jul 26 '07 #9
Alex Popescu a écrit :
Bruno Desthuilliers <bd*****************@free.quelquepart.frwrote in
news:46***********************@news.free.fr:
>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 !-)
./alex
--
.w( the_mindstorm )p.

Jul 26 '07 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Rajarshi Guha | last post: by
4 posts views Thread by Neil Zanella | last post: by
reply views Thread by Alex Martelli | last post: by
6 posts views Thread by Andre Meyer | last post: by
3 posts views Thread by dischdennis | last post: by
12 posts views Thread by pythoncurious | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.