Last active
December 18, 2015 03:08
-
-
Save ionelmc/5715543 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): | |
born = False | |
def __call__(cls, *args, **kwargs): | |
if cls.born: | |
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.born = man = super(Highlander, cls).__call__(*args, **kwargs) | |
return man | |
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