class or thread id count

NighterNet darkneter at gmail.com
Thu Jul 30 16:09:36 EDT 2009


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
>
> > example:
> > class ClientThread (threading.Thread):
> >    def __init__(self,clientSocket):
> >       threading.Thread.__init__(self)
> >          self.id += 1;
>
> > Not sure it how it works.
>
> use a class atrribute
>
> >>> class Person():
>
>         count = 0
>         def __init__(self, name, info):
>                 self.name = name
>                 self.info = info
>                 Person.count+=1
>
> >>> p1 = Person('alex23', 'very annoying')
> >>> p2 = Person('Guido', 'very intelligent')
> >>> p2.count
> 2
> >>> p1.count
> 2
> >>> Person.count
>
> 2

Thanks it work.

class Person():
  count = 0
  def __init__(self, name, info):
      self.name = name
      self.info = info
      Person.count+=1
      self.count = Person.count



More information about the Python-list mailing list