472,958 Members | 1,855 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 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 1985
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: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.