[PYTHON] Calculation of homebrew class and existing class

I want to perform multiplication of my own class and an existing class

For example, what should I do when I want to execute a constant multiple of the "Point class" that is often given as an example? As an example

class Point():
    def __init__(self,x,y):
        self.x = x
        self.y = y

Let's define multiplication though. As a specification, let's assume that when multiplying points, a Point type that multiplies by x and y is returned, and when multiplying with an integer, a Point type that is multiplied by a constant is returned.

Conclusion

First, define the operation with \ _ \ _ mul__

class Point():
...
    def __mul__(self, other):
        if isinstance(other,Point):
            return Point(self.x*other.x,self.y*other.y)
        elif isinstance(other,int):
            return Point(self.x*other,self.y*other)
        else:
            return NotImplemented
...

Since this alone does not support operations from the right, use \ _ \ _ rmul__ For example, if mul is defined in an operation that holds the commutative law.


def __rmul__(self,other):
   return self*other

But it's okay if you write If you want to use other * = etc., you can call \ _ \ _ imul__.

Recommended Posts

Calculation of homebrew class and existing class
Example of using class variables and class methods
Comparison of class inheritance and constructor description
Calculation of elapsed years and elapsed months including leap years
Calculation of technical indicators by TA-Lib and pandas
[Python] Class type and usage of datetime module
Summary of pickle and unpickle processing of user-defined class
About Class and Instance
parallelization of class method
Calculation of standard deviation and correlation coefficient in Python
[python] Calculation of months and years of difference in datetime
Class methods and static methods
Start numerical calculation in Python (with Homebrew and pip)
Sample of getting module name and class name in Python
Overview of class-based generic views and inherited class relationships
[Python] Types of statistical values (features) and calculation methods
[Python] Heron's formula functionalization and calculation of the maximum area
Calculation of odometry using CNN and depth estimation Part 2 (CNN SLAM # 2)
Reference order of class variables and instance variables in "self. Class variables" in Python
[Introduction to Scipy] Calculation of Lorenz curve and Gini coefficient ♬
Derivation of EM algorithm and calculation example for coin toss
Problems of liars and honesty
Various class methods and static methods
Mechanism of pyenv and virtualenv
Pre-processing and post-processing of pytest
Globalization of class instance variables
Combination of anyenv and direnv
Explanation and implementation of SocialFoceModel
Differentiation of sort and generalization of sort
Calculation of similarity by MinHash
Python class variables and instance variables
Coexistence of pyenv and autojump
Use and integration of "Shodan"
Problems of liars and honesty
About cost calculation of MeCab
Occurrence and resolution of tensorflow.python.framework.errors_impl.FailedPreconditionError
Class inheritance and super functions
Comparison of Apex and Lamvery
Source installation and installation of Python
Introduction and tips of mlflow.Tracking
Estimator calculation / prediction at Lasso and Ridge of generalized linear model