user8920020
user8920020

Reputation:

Calling method from another method in the same class: which parameter is Python asking for?

I want to make a class, and make several methods inside that class. I want one method to take a list and multiply it by a number, and another method to take that same number and multiply it by the list.

I am trying to call __ mul __ from inside __ rmul __, since they are using the same parameters (list and number) but in reversed order.

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__

Why do I keep getting the error message "name 'thor' is not defined"? Once it gets to the __ rmul __ method, how do I tell Python to go use the __ mul __ method?

Upvotes: 2

Views: 3875

Answers (1)

Nick Larsen
Nick Larsen

Reputation: 18877

In the __rmul__ function there is no thor parameter, there is a loki parameter only.

Try using that parameter to call __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__

Upvotes: 2

Related Questions