class or thread id count

N

NighterNet

Need some help on doing some bit simple id count. It like every time
it create a class or thread there 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.
 
R

r

Need some help on doing some bit simple id count. It like every time
it create a class or thread there 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
count = 0
def __init__(self, name, info):
self.name = name
self.info = info
Person.count+=1

2
 
N

NighterNet

use a class atrribute


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


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
 
G

Gabriel Genellina

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,201
Messages
2,571,048
Members
47,650
Latest member
IanTylor5

Latest Threads

Top