Skip to content

Instantly share code, notes, and snippets.

@scandiumby
Last active September 24, 2019 09:48
Show Gist options
  • Save scandiumby/23b45dffd424d302632534d215461624 to your computer and use it in GitHub Desktop.
Save scandiumby/23b45dffd424d302632534d215461624 to your computer and use it in GitHub Desktop.
Example for Class counter in Python3
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())
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