Counting number of objects

Kottiyath n.kottiyath at gmail.com
Sun Jan 25 18:23:35 CET 2009


Hi,
I am creating a class called people - subclasses men, women, children
etc.
I want to count the number of people at any time.
So, I created code like the following:

class a(object):
    counter = 0
    def __new__(cls, *args, **kwargs):
        a.counter += 1
        return object.__new__(cls, *args, **kwargs)

    def __del__(self):
        a.counter -= 1

class aa(a):
    pass

Now, the code works Ok. I have the following questions:
1. Is this code Ok? Is there any straightforward mechanism other than
this to get the number of objects?
2. I read in Python Documentation that inside __del__ we should the
minimum of interaction with external parameters. So, I am a little
worried in subclassing __del__ to check the counter. Is whatever I
have done Ok?

Another question - unrelated to the major topic:
How much time does it take to be proficient in Python? I have been
working exclusively in Python for close to 3 months now, and even now
I get inferiority complex when I read the answers sent by many of you.
I have been programming for close to 7 years now (earlier in a
language similar to COBOL).
Does it take quite a bit of time to be proficient - as many of you
guys - or am I just dumb?



More information about the Python-list mailing list