Skip to content

Instantly share code, notes, and snippets.

@AndrewDowning
Created July 9, 2013 15:04
Show Gist options
  • Save AndrewDowning/5958103 to your computer and use it in GitHub Desktop.
Save AndrewDowning/5958103 to your computer and use it in GitHub Desktop.
Borg pattern demo
class Borg(object):
_state = {}
def __new__(cls, *p, **k):
self = object.__new__(cls, *p, **k)
self.__dict__ = cls._state
return self
class MyBaseClass(object):
def __init__(self, base_thing):
self.base_thing = base_thing
class MyBorgClass(Borg, MyBaseClass):
def __init__(self, thing=None):
if thing:
MyBaseClass.__init__(self, "Base_"+thing)
self.thing = thing
class Subordinate(object):
def __init__(self):
print("Subordinate accessing Base",MyBorgClass().base_thing)
first_instance = MyBorgClass("Original")
second_instance = MyBorgClass()
print (first_instance.thing, second_instance.thing)
sub = MyBorgClass.Subordinate()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment