函数numpy.r_r如何包含[]



我认为python中的function必须使用()符号来获取参数。

numpy.r_似乎并没有遵循这一规律这怎么可能

numpy.r_不是函数,而是RClass的对象。Python允许您通过重载类中的相关方法来定义运算符的自定义行为。例如,您可以通过重载__getitem__方法为类对象定义[]运算符的行为。

class Squares:
def __getitem__(self, index):
return index ** 2

squares = Squares()
print(squares[1]) # 1
print(squares[2]) # 4
print(squares[3]) # 9

有关详细信息,请参阅本教程:https://realpython.com/operator-function-overloading/

最新更新