Last active
September 24, 2019 09:48
-
-
Save scandiumby/23b45dffd424d302632534d215461624 to your computer and use it in GitHub Desktop.
Example for Class counter in Python3
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 EJCounter: | |
counter = 0 | |
def __init__(self): | |
EJCounter.counter += 1 | |
def __str__(self): | |
return f'{self.__class__.__name__} was called {self.counter} time' | |
if __name__ == "__main__": | |
[EJCounter(), EJCounter()] | |
print(EJCounter()) |
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 EJCounter: | |
counter = 0 | |
@classmethod | |
def create_new_instance(cls): | |
EJCounter.counter += 1 | |
return cls() | |
def __str__(self): | |
return f'{self.__class__.__name__} was called {self.counter} time' | |
if __name__ == "__main__": | |
[EJCounter.create_new_instance(), EJCounter.create_new_instance()] | |
print(EJCounter()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment