我认为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/