Created
June 5, 2013 17:07
-
-
Save ionelmc/5715527 to your computer and use it in GitHub Desktop.
This file contains 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 Highlander(type): | |
immortal = None | |
def __call__(cls, *args, **kwargs): | |
if cls.immortal is not None: | |
raise RuntimeError("THERE CAN BE ONLY ONE !") # You cannot make more than 1 instance of a Highlander class, it's too dangerous to have 2 ! | |
cls.immortal = super(Highlander, cls).__call__(*args, **kwargs) | |
return cls.immortal | |
class MacLeod(object): | |
__metaclass__ = Highlander | |
man = MacLeod() | |
man2 = MacLeod() # raises epic RuntimeError |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment