Counting number of objects

Andreas Waldenburger geekmail at usenot.de
Sun Jan 25 13:06:47 EST 2009


On Sun, 25 Jan 2009 09:23:35 -0800 (PST) Kottiyath
<n.kottiyath at gmail.com> wrote:

> 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
> 
This looks OK, although I'd suggest using "cls.counter += 1" instead of
"a.counter += 1" in the __new__() method. Just seems clearer to me,
esp. when you think about subclassing. This would create an asymmetry
with __del__() then. Oh well. So maybe use "self.__class__.counter -= 1"
there, even if it is a bit ugly-ish.

Another way to go would be to use the weakref module and create a
weakref-set (or list) as the counter. That way you would
only need to add the objects in the __new__() method and not worry
about removing them. I will admit that this is overengineering the
problem a bit, but might be a good exercise.

A third option could be to remove the counting functions from the class
altogether. From an OO-Design point of view this would seem
appropriate, because neither any individual nor mankind as a whole
would know the exact amount of people in the world of hand. An entity
that would actually count all the people in the world, however, would
know and it makes sense to implement it separately (as a subclass of
list with birth() and death() methods, for instance). I'm just saying
this to give you something to think about, I'm not saying that it is
necessarily better or worse for your example.


> 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?
I would say that you found the most obvious implementation there.
Depending on your definition of "straightforward" you could do it as I
outlined in my last example above, using explicit calls for births and
deaths. This would remove the "behind the scenes" magic a bit, which
may be a plus.


> 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?
> 
Again, seems good to me, unless you do some other trickery that may
lead to __del__() not being called directly. In that case, do look at
the weakref module.


> Another question - unrelated to the major topic:
> How much time does it take to be proficient in Python?
Don't concern yourself with that question at all, is my advice.

Most people can learn to write programs in under a week, and many still
don't write *good* programs 20 years later. You'll get better over
time, as long as you keep doing it. Thinking about that you're not good
enough will just consume mental resources that would be better invested
in your programs.


> 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.
Hello?! :)

Three months is *nothing* compared to the time those "many of you"
folks have invested. Don't worry. Does it matter if you don't
understand some stuff people write? As long as you pick out the stuff
you do understand you're still gaining stuff from this group.


> I have been programming for close to 7 years now (earlier in a
> language similar to COBOL).
Well, you have quite a background then. Why all the worries?


> Does it take quite a bit of time to be proficient - as many of you
> guys
YES! Of course it does.


> - or am I just dumb?
You're writing programs and you're communicating with like-minded
people about your problems (in a socially appropriate way). Not what
dumb people do, in my book.


cheers,
/W

-- 
My real email address is constructed by swapping the domain with the
recipient (local part).



More information about the Python-list mailing list