Skip to content

Instantly share code, notes, and snippets.

@hwmrocker
Created February 19, 2015 20:29
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 hwmrocker/45bd2d25fff38ca80f26 to your computer and use it in GitHub Desktop.
Save hwmrocker/45bd2d25fff38ca80f26 to your computer and use it in GitHub Desktop.
class UniqueMeta(type):
def __new__(cls, *foo, **kw):
tmp = type.__new__(cls, *foo, **kw)
tmp._COUNTER = 0
return tmp
class UniquePizza(metaclass=UniqueMeta):
def __init__(self):
self.id = self._get_next_id()
@classmethod
def _get_next_id(cls):
cls._COUNTER += 1
print(repr(cls))
return cls._COUNTER
class UniquePeperoniPizza(UniquePizza):
pass
if __name__ == "__main__":
a = UniquePizza()
b = UniquePizza()
p = UniquePeperoniPizza()
print("a {}, b {}, p {}".format(a.id, b.id, p.id))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment