Given the following class
class sq():
def __init__(self,func,x):
self.func = func
self.x = x
def square(self):
return self.func(self.x)**2
y = lambda x,p: x + p
p_ls = range(1,11)
x = 0
objects = [sq(lambda x: y(x,pi),x) for pi in p_ls]
squares = [sq(lambda x: y(x,pi),x).square() for pi in p_ls]
#[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
squares1 = [obj.square() for obj in objects]
#[100, 100, 100, 100, 100, 100, 100, 100, 100, 100]
i can't understand why squares
are not equal to squares1
. Its seems like all objects in objects
list are using p = p_ls[-1]
. Is there a way to "store" func
each time the class is called ?