I am trying to generate a pseudo random number every time I create a new object object from a specific Class.
import random
class Test (object):
def __init__ (self,value=random.randint(0,10)):
self.value=value
this works fine when instantiating the first object from the class, for example
test1=Test()
will give a random integer to the attribute. So the attribute, test1.value
will indeed be assigned a random number.
however, any additional objects created from the class will not generate a new random number, but instead, keep the value of the first. So all my additional objects from the class, have the same identical integer value for their .value
attribute as the first object. I would have expected the part of the code that generates the random number to run again each time a new object is created.
Feel I'm missing something obvious