我想制作一个类,并在该类中制作几个方法。我想要一种方法获取列表并将其乘以数字,另一种方法获取相同的数字并将其乘以列表。
我正在尝试从 __ rmul __ 内部调用 __ mul __,因为它们使用相同的参数(列表和数字)但顺序相反。
class Asgard(object):
def __init__(self, list=0)
self.list = copy.deepcopy(list)
def __mul__(self, thor):
#multiplying list by integer
def __rmul__(self, loki):
self.__mul__(thor) #calling the method __mul__
为什么我不断收到错误消息“名称‘thor’未定义”? 一旦到达 __ rmul __ 方法,我如何告诉 Python 去使用 __ mul __ 方法?
请您参考如下方法:
__rmul__函数中没有thor参数,只有loki参数。
尝试使用该参数调用 __mul__:
class Asgard(object):
def __init__(self, list=0)
self.list = copy.deepcopy(list)
def __mul__(self, thor):
#multiplying list by integer
def __rmul__(self, loki):
self.__mul__(loki) #calling the method __mul__
