Created
November 16, 2016 21:06
-
-
Save adammartinez271828/fa3df100bbf0c8a043327a368065f3f7 to your computer and use it in GitHub Desktop.
Borg Class Implementation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Borg(object): | |
"""A Borg class object | |
This object forces all instances of it to share the same internal state. | |
It is subclassable. Each subclass only shares monostate with itself, i.e.: | |
Borg classes do not share internal state with their subclasses. | |
""" | |
__monostate = None | |
def __init__(self, *args, **kwargs): | |
"""Initialize a borg object | |
Pass all arguments to the initialize function, if and only if the Borg | |
does not already have a monostate, else, return a new Borg object with | |
the current monostate. | |
""" | |
if Borg.__monostate is not None: | |
self.__dict__ = Borg.__monostate | |
else: | |
Borg.__monostate = self.__dict__ | |
self.initialize(*args, **kwargs) | |
def initialize(self, *args, **kwargs): | |
"""Init function for a Borg object | |
Invoked the first time a Borg object is made. Allows setting some | |
initial state of the objects. | |
""" | |
pass | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment