2

I want to create multiple bots with all their own unique id. But how can do this automatically for numerous bots and have all an other id? I can use bot1, bot2 but what if i want to use this with 100 bots?

class newbot:
 id = randomid()
bot1 = newbot() 
bot2 = newbot() 
print bot1.id
print bot2.id #all the same id
asked May 28, 2011 at 9:12

2 Answers 2

7

The id member ends up being shared among all instances of your class because it's defined as a class member instead of an instance member. You probably should write:

class newbot(object):
 def __init__(self):
 self.id = randomid()
bot1 = newbot() 
bot2 = newbot() 
# The two ids should be different, depending on your implementation of randomid().
print bot1.id
print bot2.id 
answered May 28, 2011 at 9:20
Sign up to request clarification or add additional context in comments.

Comments

3

Use built-in function id() for this. Construct new empty object and get id( obj ).

Or you can get id( bot1), id( bot2 )

Try the next:

 def getRandId( self ):
 return id( self )
answered May 28, 2011 at 10:33

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.