I recently found this strange Python error, and I wanted to know if anyone knew about this!
for example take the python module:
import random class SaySomething: def __init__(self, value=random.randint(1, 3)): if value == 1: print 'one' elif value == 2: print 'two' elif value == 3: print 'three' a = 0 while a < 10: SaySomething() a += 1
This code for any reason will print YOUR number 10 times !!! Now I dont understand. It seems that the constructor is being called with the same values ββ10 times in a row. But if you print each SaySomething() , you will see that they all have different pointer addresses, so they are not the same object.
Now if you change:
SaySomething()
to
SaySomething(random.randint(1, 3))
Works as expected with actual random choices.
Does anyone know why this is happening?
source share