我想创建一个类piecewise_func,它的工作方式如下
F = piecewise_func(0,100)(10,122)我可以添加如下的值
F = F(30, 0)得到y值就像:
F.y(5) 'y = 111‘我怎么能意识到这一点?)我试着让
class piecewise_func:
def __init__(self, x:list, y:list):
self._x = x
self._y = y
def y(self, x):
pass
def __call__(self, x, y):
return piecewise_func(self._x + x, self._y + y)
def __str__(self):
return str(self._y)我知道它只是x和y,但我不知道如何扩展我的x和y列表,谢谢!
发布于 2022-06-24 15:50:05
重新编辑适当的评论:
class piecewise_func:
def __init__(self, x:int, y:int):
self._x = [x]
self._y = [y]
def y(self, x):
pass
def __call__(self, x, y):
self._x += [x] # update the lists
self._y += [y]
return self
def __str__(self):
return f'x: {self._x}\ny: {self._y}'
F = piecewise_func(0, 100)
G = F(10, 122)
H = G(1, 2)
print(F)
#x: [0, 10, 1]
#y: [100, 122, 2]注意F,G,H,.将指向同一个对象,因此也可以使用print(G)或print(H)。
https://stackoverflow.com/questions/72746199
复制相似问题