object member assignment in python -
this question has answer here:
class func: def __init__(self): self.weight = 0 class skill: def __init__(self, bson): self.f = [func()]*5 in range(0,5): self.f[i].weight = bson['w'+str(i+1)] d = {"w1":20,"w2":0,"w3":0,"w4":0,"w5":0} s = skill(d) print s.f[0].weight
the output 0 rather 20. hard figure out difference of class member , object member in python.
you should not create list of func
in way
self.f = [func()]*5
you should use list comprehension or other method
self.f = [func() _ in range(5)]
in first method, creating list
of length 5, elements pointing same instance of func
. don't want this, want create list
of 5 different instances of func
.
Comments
Post a Comment