【发布时间】:2020-04-01 11:06:50
【问题描述】:
我正在尝试自定义我自己的迭代类,并尝试将其插入计算:
class Iteration:
def __init__(self, array):
self.array = array
def __pow__(self, power, modulo=None):
new_array = list()
for i in self.array:
new_array.append(i ** power)
return new_array
def __len__(self):
return len(self.array)
def __getitem__(self, indices):
return self.array[indices]
def mul(x):
return x ** 2 + 3 * x ** 3
it = Iteration([1, 2, 3])
print(mul(2)) #=> 28
print(mul(it)) #=> [1, 4, 9, 1, 8, 27, 1, 8, 27, 1, 8, 27]
为什么 mul(it) 合并了重载结果?我该如何解决这个问题? 我想: print(mul(it)) #=> [4, 28, 90]
【问题讨论】:
-
当 x 为
it时,x**2的值是什么?