473,471 Members | 1,912 Online
Bytes | Software Development & Data Engineering Community
Create 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 "instantiation" (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(object): ...
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 2081
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 "instantiation" (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
misunderstanding 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 "instantiation" (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(object):
def __new__(cls):
self = getattr(cls,"_instance",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(object):
# nothing odd

def Factory():
obj = getattr(_NotOddClass,"_instance",None)
if obj is None:
obj = _NotOddClass._instance = 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...@gmail.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 "instantiation" (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
misunderstanding 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
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...
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...
15
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...
6
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...
2
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...
10
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...
2
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...
0
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
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...
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
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...
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...
1
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
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...

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.