Is this a good way to know how many instance that has been created? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Is this a good way to know how many instance that has been created?

This is my program which contain class with class variable named n for checking how many instance that has been created. At first, inside __init__ I write self.n+=1. But, it makes the instance variable change and not class variable. That's why I changed it to tes.n+=1. Is it allowed? Is there any different way to do it? Because I feel like my program is kinda ugly.... https://code.sololearn.com/cNm3LP1miYAN/?ref=app

28th Jun 2017, 7:21 PM
Ahmad Fahadh Ilyas
Ahmad Fahadh Ilyas - avatar
1 Answer
0
look up singleton pattern examples in Python. You will need to have a private inner class doing the heavy lifting. the outer class will maintain the counter. also lookup __new__(). it could come in handy in these use cases.
28th Jun 2017, 9:06 PM
Venkatesh Pitta
Venkatesh Pitta - avatar