Skip to content

Instantly share code, notes, and snippets.

@ionelmc
Last active December 18, 2015 03:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ionelmc/5715543 to your computer and use it in GitHub Desktop.
Save ionelmc/5715543 to your computer and use it in GitHub Desktop.
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