class or thread id count

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Fri Jul 31 15:45:53 EDT 2009


En Thu, 30 Jul 2009 17:09:36 -0300, NighterNet <darkneter at gmail.com>
escribió:
> On Jul 30, 12:14 pm, r <rt8... at gmail.com> wrote:
>> On Jul 30, 1:13 pm, NighterNet <darkne... at gmail.com> wrote:
>>
>> > Need some help on doing some bit simple id count. It like every time
>> > it create a class orthreadthere is id++. Python 3.1
>>
>> use a class atrribute
>
> class Person():
>   count = 0
>   def __init__(self, name, info):
>       self.name = name
>       self.info = info
>       Person.count+=1
>       self.count = Person.count

Since you menctioned "thread" in the subject, note that the above isn't
thread-safe. You'll need a lock around the last two statements.

-- 
Gabriel Genellina




More information about the Python-list mailing list