Created
February 19, 2015 20:29
-
-
Save hwmrocker/45bd2d25fff38ca80f26 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 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