Someone help me with logic for writing program for count the number of objects present within class without using the constructors.
Asked
Active
Viewed 266 times
0
-
I think you can find what you need here: https://stackoverflow.com/questions/8628123/counting-instances-of-a-class – Adam Bellaïche Nov 27 '19 at 10:13
-
There they're using __init__ method. Without using the __init__ method I need the logic – Sridhar Selvam Nov 27 '19 at 10:20
-
Why do you need to count the number of objects without using constructors? Because you can just create a fonction which create the object and count it. But it's a little "weird". – Adam Bellaïche Nov 27 '19 at 10:29
-
You can also store you're object in a list and get the length to know the number – Adam Bellaïche Nov 27 '19 at 10:34
-
I attended one interview and they asked to write the program without using Constructors to count the number objects present within the class. I don't know how iterate it in loop. That's why I'm asking here...... – Sridhar Selvam Nov 27 '19 at 10:41
1 Answers
0
You can use a fonction but I maintain it's a little weird:
class test:
def __init__(self, name):
self.name = name
def createAndCount(name_object, counter):
counter+=1
return [test(name_object), counter]
counter = 0
p1, counter = createAndCount("Ronflex", counter)
p2, counter = createAndCount("Doudou", counter)

Adam Bellaïche
- 427
- 3
- 16