Re: Counting number of objects

2009-01-27 Thread Scott David Daniels
Kottiyath wrote: So, in a higher level class, have a weakref list which contains a reference to each person. Total count will be len(list) at any time. Now, I couldnt find a weakref list - so I am using WeakKeyDictionary with the value as None - since len(dict) also should give me the data I ty

Re: Counting number of objects

2009-01-27 Thread Kottiyath
Thank you everyone for your very helpful comments and suggestions. I have interacted in other newsgroups, but this is the most helpful of them all. As per the comments, I have now decided to go with the weakref mechanism - as per Andreas suggestion, functionally it looks correct that the person sh

Re: Counting number of objects

2009-01-26 Thread Steve Holden
Mark Wooding wrote: > Andreas Waldenburger writes: > >> On Sun, 25 Jan 2009 09:23:35 -0800 (PST) Kottiyath >> wrote: >> >>> class a(object): >>> counter = 0 >>> def __new__(cls, *args, **kwargs): >>> a.counter += 1 >>> return object.__new__(cls, *args, **kwargs) > > Hmm.

Re: Counting number of objects

2009-01-26 Thread Andreas Waldenburger
On Mon, 26 Jan 2009 02:37:37 + Mark Wooding wrote: > > 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. > > I'm not sure about clarity, but that would be

Re: Counting number of objects

2009-01-25 Thread Mark Wooding
Andreas Waldenburger writes: > On Sun, 25 Jan 2009 09:23:35 -0800 (PST) Kottiyath > wrote: > >> class a(object): >> counter = 0 >> def __new__(cls, *args, **kwargs): >> a.counter += 1 >> return object.__new__(cls, *args, **kwargs) Hmm. Exceptions raised during object cr

Re: Counting number of objects

2009-01-25 Thread Gabriel Genellina
En Sun, 25 Jan 2009 16:06:47 -0200, Andreas Waldenburger escribió: On Sun, 25 Jan 2009 09:23:35 -0800 (PST) Kottiyath wrote: 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:

Re: Counting number of objects

2009-01-25 Thread Gabriel Genellina
En Sun, 25 Jan 2009 16:06:47 -0200, Andreas Waldenburger escribió: On Sun, 25 Jan 2009 09:23:35 -0800 (PST) Kottiyath wrote: 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:

Re: Counting number of objects

2009-01-25 Thread Andreas Waldenburger
On Sun, 25 Jan 2009 09:23:35 -0800 (PST) Kottiyath 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

Re: Counting number of objects

2009-01-25 Thread Steve Holden
Kottiyath 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 +=

Counting number of objects

2009-01-25 Thread Kottiyath
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,