473,385 Members | 1,907 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 2001
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Rajarshi Guha | last post by:
Hi, I'm having a little problem with understanding the working of a singleton and borg class. Basically I nedd an class whose state will be shared across several modules. I found the stuff on the...
0
by: John J Lee | last post by:
QOTW: "...simple genexps will work fine anyway almost all the time, so it doesn't appear to matter much that devious uses will have nightmarish semantics." -- Tim Peters on Generator Expressions ...
0
by: Peter Otten | last post by:
QOTW: "I worry about Python losing its KISS principle. Python 2.2 has been ported to the Nokia Series 60 platform, which has something like 8-16 MB of RAM available. I'm sure the footprint growth...
4
by: Neil Zanella | last post by:
Hello, I would be very interested in knowing how the following C++ multi-instance singleton (AKA Borg) design pattern based code snippet can be neatly coded in Python. While there may be...
0
by: Alex Martelli | last post by:
Greetings, fellow Pythonistas! We (Alex Martelli, David Ascher and Anna Martelli Ravenscroft) are in the process of selecting recipes for the Second Edition of the Python Cookbook. Please...
10
by: Simon Brunning | last post by:
QOTW: "I think my code is clearer, but I wouldn't go so far as to say I'm violently opposed to your code. I save violent opposition for really important matters like which text editor you use." -...
6
by: Andre Meyer | last post by:
While looking for an elegant implementation of the singleton design pattern I came across the decorator as described in PEP318<http://www.python.org/dev/peps/pep-0318/> . Unfortunately, the...
3
by: dischdennis | last post by:
Hello List, I would like to make a singleton class in python 2.4.3, I found this pattern in the web: class Singleton: __single = None def __init__( self ): if Singleton.__single: raise...
12
by: pythoncurious | last post by:
Hi, I've been trying to get some sort of singleton working in python, but I struggle a bit and I thought I'd ask for advice. The first approach was simply to use a module, and every variable...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.