【问题标题】:Overloading Addition, Subtraction, and Multiplication OperatorsOverloading Addition, Subtraction, and Multiplication Operators
【发布时间】:2022-12-02 07:19:15
【问题描述】:

How do you go about overloading the addition, subtraction, and multiplication operator so we can add, subtract, and multiply two vectors of different or identical sizes? For example, if the vectors are different sizes we must be able to add, subtract, or multiply the two vectors according to the smallest vector size?

I've created a function that allows you to modify different vectors, but now I'm struggling to overload the operators and haven't a clue on where to begin. I will paste the code below. Any ideas?

def __add__(self, y):
    self.vector = []
    for j in range(len(self.vector)):
        self.vector.append(self.vector[j] + y.self.vector[j])
    return Vec[self.vector]

【问题讨论】:

    标签: python class vector operators


    【解决方案1】:

    You define the __add__, __sub__, and __mul__ methods for the class, that's how. Each method takes two objects (the operands of +/-/*) as arguments and is expected to return the result of the computation.

    【讨论】:

    • okay, i understand that now. thank you. how do i actually define them in this code? self.vector[index].__add__(self, x)???
    • @user3014014: The same way you defined the other methods; just stick them in the class definition.
    • would def __add__(self, x, y) have 2 arguments like this?
    • @user3014014: No; it's def __add__(self, y). The self is the left operand to +.
    • And to divide, use __truediv__. See docs.python.org/3/reference/… for more.
    【解决方案2】:

    Nothing wrong with the accepted answer on this question but I'm adding some quick sn-ps to illustrate how this can be used. (Note that you could also "overload" the method to handle multiple types.)


    """Return the difference of another Transaction object, or another 
    class object that also has the `val` property."""
    
    class Transaction(object):
    
        def __init__(self, val):
            self.val = val
    
        def __sub__(self, other):
            return self.val - other.val
    
    
    buy = Transaction(10.00)
    sell = Transaction(7.00)
    print(buy - sell)
    # 3.0
    

    """Return a Transaction object with `val` as the difference of this 
    Transaction.val property and another object with a `val` property."""
    
    class Transaction(object):
    
        def __init__(self, val):
            self.val = val
    
        def __sub__(self, other):
            return Transaction(self.val - other.val)
    
    
    buy = Transaction(20.00)
    sell = Transaction(5.00)
    result = buy - sell
    print(result.val)
    # 15
    

    """Return difference of this Transaction.val property and an integer."""
    
    class Transaction(object):
    
        def __init__(self, val):
            self.val = val
    
        def __sub__(self, other):
            return self.val - other
    
    
    buy = Transaction(8.00)
    print(buy - 6.00)
    # 2
    

    【讨论】:

    • how do you overload method to accept multiple types?
    【解决方案3】:

    docs have the answer. Basically there are functions that get called on an object when you add or multiple, etc. for instance __add__ is the normal add function.

    【讨论】:

      猜你喜欢
      • 2022-12-01
      • 2020-04-01
      • 1970-01-01
      • 2023-04-05
      • 1970-01-01
      • 2022-06-26
      • 1970-01-01
      • 2017-03-24
      • 2014-03-04
      相关资源
      最近更新 更多