473,809 Members | 2,940 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Borg vs Singleton vs OddClass

Lie
This is probably unrelated to Python, as this is more about design
pattern. I'm asking your comments about this design pattern that is
similar in functionality to Singleton and Borg: to share states.

I'm thinking about this design pattern (I don't know if anyone has
ever thought of this pattern before):

class OddClass(object ):
def __init__(self):
global OddClass
OddClass = self
def __call__():
return self

The OddClass is a class that would overwrite its own class definition
at its first instantiation. OddClass defines __call__ so that
subsequent "instantiat ion" (technically it is no more an
instantiation, but Duck Typing says it does) of the class would return
the single instance.

It do have a problem though, that you can't do isinstance(a, OddClass)
since the name OddClass no longer refers to the OddClass class
descriptor, but to an instance of OddClass. I don't think that's much
of a problem though since using isinstance() is generally not a good
idea in python (although OddClass do use global which many people,
including me, would usually consider as bad form).

The problem with Borg is that it is not inheritable (in certain
sense[1]) and only work with old-style class (which is due to be
completely removed on py3k)[2], Singleton and OddClass is inheritable.
>>class Singleton(objec t): ...
s = Singleton()
class A(Singleton): pass
class B(Singleton): pass
a = A()
b = B()
s.foo, a.bar = "bar", 42
b.foo, b.bar
.... Traceback, Attribute Error ...
>>class OddClass(object ): ...
s = OddClass()
class A(OddClass): pass
class B(OddClass): pass
a = A()
b = B()
s.foo, a.bar = "bar", 42
b.foo, b.bar
.... Traceback, Attribute Error ...

but for Borg, see [1]

[1] classes that inherits from Borg shares state not only within
children, but also with their parents and cousins. That means
inheriting from Borg is useless, and that also means one Borg code for
every state sharing classes, instead of inheriting from Singleton/
OddClass. In code:
>>class Borg: ...
class A(Borg): ...
class B(Borg): ...
s, a, b = Borg(), A(), B()
s.foo, a.attr = "bar", 42
b.foo, b.attr
"bar" 42
>># we want b.foo and b.attr to raise error since .foo, and .bar isn't B's shared state, it's A's and Borg's shared state
[2] Actually in new-style class, they say Borg can use __slots__, but
they say it'd be slightly more complex
Sep 27 '08 #1
5 2109
Lie wrote:
This is probably unrelated to Python, as this is more about design
pattern. I'm asking your comments about this design pattern that is
similar in functionality to Singleton and Borg: to share states.

I'm thinking about this design pattern (I don't know if anyone has
ever thought of this pattern before):

class OddClass(object ):
def __init__(self):
global OddClass
OddClass = self
def __call__():
I'll change this to def __call__(self):
return self

The OddClass is a class that would overwrite its own class definition
at its first instantiation. OddClass defines __call__ so that
subsequent "instantiat ion" (technically it is no more an
instantiation, but Duck Typing says it does) of the class would return
the single instance.
This seems like a terrible idea to me, but then I never really
understood the appeal of the Singleton pattern, especially in Python.
Singleton and OddClass is inheritable.
>>>class OddClass(object ): ...
s = OddClass()
class A(OddClass): pass
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: Error when calling the metaclass bases
__init__() takes exactly 1 argument (4 given)

Oops! And assuming you carefully ordered your code so that OddClass
will never be instantiated before it is subclassed (which seems
fragile), you get behavior like this:
>>s = OddClass()
s is OddClass()
True
>>a = A()
s is OddClass()
False
>>a is OddClass()
True
>>a is A()
False
>>a is OddClass()
False

-Miles
Sep 28 '08 #2
On Sat, 27 Sep 2008 11:12:00 -0700, Lie wrote:
This is probably unrelated to Python, as this is more about design
pattern. I'm asking your comments about this design pattern that is
similar in functionality to Singleton and Borg: to share states.

I'm thinking about this design pattern (I don't know if anyone has ever
thought of this pattern before):

class OddClass(object ):
def __init__(self):
global OddClass
OddClass = self
def __call__():
return self

I don't think that pattern works as you give it. I suggest you read the
thread "What do you call a class not intended to be instantiated",
started by me on the 21st of September, which covers a similar pattern.

I'm afraid it's a rather long thread, with a lot of people
misunderstandin g what I was asking, but still worth reading. If you only
have time to read one post, I suggest you read my reply to Ben Finney,
posted yesterday.

My own feeling is that both your OddClass and my class without instances
are probably solutions looking for a problem. Well, actually, no, that's
too strong: I think the concept of "Class Singleton" is a perfectly valid
solution to certain problems, but it competes with more well-known
solutions like modules and Borg (in Python) and Singletons (the hammer
available in Java and C++). As for which is better, that's partly a
matter of personal taste and partly a matter of familiarity.

It do have a problem though, that you can't do isinstance(a, OddClass)
But you can say "a is OddClass", which is more appropriate for a
Singleton.
The problem with Borg is that it is not inheritable (in certain
sense[1]) and only work with old-style class (which is due to be
completely removed on py3k)[2]
No, there is a new-style Borg. Read the comments here:
http://code.activestate.com/recipes/66531/

The new-style Borg is hardly more complicated than old-style: 6 lines
versus 4.

I like Luke Plant's comment:

"classes and modules are singletons. You don't need singletons in python
simply because classes and modules are always singletons, and they are
also first class objects. They can have everything instances have, and as
import statements don't make copies there is only ever one of them. We
don't need no stinkin' design patterns."

--
Steven
Sep 28 '08 #3
On Sep 27, 2:12*pm, Lie <Lie.1...@gmail .comwrote:
I'm thinking about this design pattern (I don't know if anyone has
ever thought of this pattern before):

class OddClass(object ):
* * def __init__(self):
* * * * global OddClass
* * * * OddClass = self
* * def __call__():
* * * * return self

The OddClass is a class that would overwrite its own class definition
at its first instantiation. OddClass defines __call__ so that
subsequent "instantiat ion" (technically it is no more an
instantiation, but Duck Typing says it does) of the class would return
the single instance.

It do have a problem though, that you can't do isinstance(a, OddClass)
since the name OddClass no longer refers to the OddClass class
descriptor, but to an instance of OddClass.
I recommend against your idiom above. The main issue I have about it
is that it rebinds the symbol implicitly, which is almost always a bad
idea. What if a user does something like "from oddclassmodule import
OddClass"? Then the user will get a new instance every call since it
never rebinds the imported symbol.

Just don't do it this way.

You could rewrite it like this to avoid the implicit rebinding, and to
take care of the isinstance issue as well:

class NotSoOddClass(o bject):
def __new__(cls):
self = getattr(cls,"_i nstance",None)
if self is None:
self = cls._instance = object.__new__( cls)
return self
Or you could just use a lazy factory function like this, where the
user is only supposed to use Factory and not create the class
directly:

class _NotOddClass(ob ject):
# nothing odd

def Factory():
obj = getattr(_NotOdd Class,"_instanc e",None)
if obj is None:
obj = _NotOddClass._i nstance = NotOddClass()
return obj
If you're real kinky you can use a metaclass. There are reasons to
prefer any of these. I'd recommend the factory function unless you
think the users could significantly benefit from type inspection.

Just don't do it by rebinding the class name. That's not nice.
Carl Banks
Sep 28 '08 #4
Lie
On Sep 28, 7:22*am, Miles <semantic...@gm ail.comwrote:
Lie wrote:
This is probably unrelated to Python, as this is more about design
pattern. I'm asking your comments about this design pattern that is
similar in functionality to Singleton and Borg: to share states.
I'm thinking about this design pattern (I don't know if anyone has
ever thought of this pattern before):
class OddClass(object ):
* *def __init__(self):
* * * *global OddClass
* * * *OddClass = self
* *def __call__():

I'll change this to def __call__(self):
* * * *return self
The OddClass is a class that would overwrite its own class definition
at its first instantiation. OddClass defines __call__ so that
subsequent "instantiat ion" (technically it is no more an
instantiation, but Duck Typing says it does) of the class would return
the single instance.

This seems like a terrible idea to me, but then I never really
understood the appeal of the Singleton pattern, especially in Python.
Singleton and OddClass is inheritable.
>>class OddClass(object ): ...
s = OddClass()
class A(OddClass): pass

Traceback (most recent call last):
* File "<stdin>", line 1, in <module>
TypeError: Error when calling the metaclass bases
* * __init__() takes exactly 1 argument (4 given)

Oops! *And assuming you carefully ordered your code so that OddClass
will never be instantiated before it is subclassed (which seems
fragile), you get behavior like this:
I test the code what would happen if I do this before posting the
pattern:
>>class OddClass(object ): ...
s = OddClass()
class A(OddClass): pass
a = A()
It doesn't give me errors, where are you having the problem?
>s = OddClass()
s is OddClass()
True
>a = A()
s is OddClass()
False
>a is OddClass()
True
>a is A()
False
>a is OddClass()

False
Well, spotted, there is identity problem with this pattern.
-Miles
Sep 28 '08 #5
Lie
On Sep 28, 9:45*am, Steven D'Aprano <st...@REMOVE-THIS-
cybersource.com .auwrote:
On Sat, 27 Sep 2008 11:12:00 -0700, Lie wrote:
This is probably unrelated to Python, as this is more about design
pattern. I'm asking your comments about this design pattern that is
similar in functionality to Singleton and Borg: to share states.
I'm thinking about this design pattern (I don't know if anyone has ever
thought of this pattern before):
class OddClass(object ):
* * def __init__(self):
* * * * global OddClass
* * * * OddClass = self
* * def __call__():
* * * * return self

I don't think that pattern works as you give it. I suggest you read the
thread "What do you call a class not intended to be instantiated",
started by me on the 21st of September, which covers a similar pattern.
In fact, that thread inspired this thread.
I'm afraid it's a rather long thread, with a lot of people
misunderstandin g what I was asking, but still worth reading. If you only
have time to read one post, I suggest you read my reply to Ben Finney,
posted yesterday.
.... before I decided probably this pattern is probably isn't the
answer to that thread.
My own feeling is that both your OddClass and my class without instances
are probably solutions looking for a problem. Well, actually, no, that's
too strong: I think the concept of "Class Singleton" is a perfectly valid
solution to certain problems, but it competes with more well-known
solutions like modules and Borg (in Python) and Singletons (the hammer
available in Java and C++). As for which is better, that's partly a
matter of personal taste and partly a matter of familiarity.
It do have a problem though, that you can't do isinstance(a, OddClass)

But you can say "a is OddClass", which is more appropriate for a
Singleton.
The problem with Borg is that it is not inheritable (in certain
sense[1]) and only work with old-style class (which is due to be
completely removed on py3k)[2]

No, there is a new-style Borg. Read the comments here:http://code.activestate.com/recipes/66531/

The new-style Borg is hardly more complicated than old-style: 6 lines
versus 4.
Sep 28 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
2654
by: Mars | last post by:
I have looked long and hard at Mr. Martelli's Borg recipe: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66531 It is a very useful substitute for a Singleton, but I can't figure out how it works. _shared_state is never assigned any value, only self.__dict__ is assigend self._shared_sate's value - or rather (self.)_shared_state must be assigned a value at some point, otherwise the code wouldn't work(right ?). I have gone...
2
2744
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 ASPN cookbook but it does'nt seem to be working for me. I've included some code: Borg.py: -------- class Borg: __shared_state = {} def __init__(self):
15
2704
by: Jay | last post by:
I'm sure this is a really dumb question, but how do you detect a variable type in Python? For example, I want to know if the variable "a" is a list of strings or a single string. How do I do this?
6
1947
by: Steven D'Aprano | last post by:
I've been working with the Borg design pattern from here: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66531 and I'm having problems subclassing it. I'm a newbie, so I've probably missed something obvious. I want two Borg-like classes where all instances share state within each class, but not between them. This is what I tried:
2
1734
by: tobiah | last post by:
I am making a web app, made up of many modules that all need access to some important data, like the current session data, cookies, navigation history, post/get variables, etc. I decided to go with the 'Borg' idea, by assigning the __dict__ of an object to a class variable so that each instantiation of the Borg() would share the same data. That way I can either pass the Borg around, or just Borg() it if I need it in some obscure place.
10
1531
by: =?iso-8859-1?B?QW5kcuk=?= | last post by:
In my application, I make use of the Borg idiom, invented by Alex Martelli. class Borg(object): '''Borg Idiom, from the Python Cookbook, 2nd Edition, p:273 Derive a class form this; all instances of that class will share the same state, provided that they don't override __new__; otherwise, remember to use Borg.__new__ within the overriden class.
2
1390
by: Tobiah | last post by:
I have a class that I call Borg that starts like this: class Borg(dict): static_state = {} def __init__(self): self.__dict__ = self.static_state so that I can access the same data from anywhere within
0
841
by: Maric Michaud | last post by:
Le Saturday 28 June 2008 03:47:43 Casey McGinty, vous avez écrit : Well, it works ! __state = {} def __init__(self): self.__dict__ = self.__state .....: .....:
1
1110
by: seanacais | last post by:
I want to create a class derived from a Borg class that can instantiated as part of a script or be contained in other classes. When methods from the Borg class are called, I would like to know the name of the class that contains the Borg class. I've played a bit with inspect and _getframe from the sys module but get inconsistent results. The main problem is if the Borg class is instantiated outside a containing class, then I need to go...
0
9722
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9603
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10391
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10121
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6881
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5690
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4333
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3862
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.