?
=?iso-8859-1?B?QW5kcuk=?=
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.
'''
_shared_state = {}
def __new__(cls, *a, **k):
obj = object.__new__(cls, *a, **k)
obj.__dict__ = cls._shared_state
return obj
----
This has worked very well so far, but is starting to impose some
unwanted constraints on my program design.
What I would like to do is, to put it figuratively, create a Borg
rebellion with various splinter groups. In concrete Python terms, I
would like to have
class MyClass(Borg, ...):
...
seven_of_nine = MyClass(...) # part of group "BORG"
two_of_nine = MyClass(...)
splinter1 = MyClass(..., group='splinter')
splinter2 = MyClass(..., group='splinter')
and have splinter 1 and splinter2 share the same state, but a
different state than the one shared by members of the BORG collective.
Any suggestions from the metaclass experts?
André
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.
'''
_shared_state = {}
def __new__(cls, *a, **k):
obj = object.__new__(cls, *a, **k)
obj.__dict__ = cls._shared_state
return obj
----
This has worked very well so far, but is starting to impose some
unwanted constraints on my program design.
What I would like to do is, to put it figuratively, create a Borg
rebellion with various splinter groups. In concrete Python terms, I
would like to have
class MyClass(Borg, ...):
...
seven_of_nine = MyClass(...) # part of group "BORG"
two_of_nine = MyClass(...)
splinter1 = MyClass(..., group='splinter')
splinter2 = MyClass(..., group='splinter')
and have splinter 1 and splinter2 share the same state, but a
different state than the one shared by members of the BORG collective.
Any suggestions from the metaclass experts?
André