[PYTHON] Special method

class Word(object):
    def __init__(self, text):
        self.text = text

    def __len__(self):
        return len(self.text)

    def __str__(self):
        return 'Read as a string'

    def __add__(self, other):
        return self.text + other.text

    def __eq__(self, other):
        return self.text == other.text


w = Word('test')
w2 = Word('test2')
print(len(w))#4
print(w)#Read as a string
print(w + w2)#testtest2
print(w == w2)#FAlse

Recommended Posts

Special method
Special method
Manim's method 7
Manim's method 13
Manim's method 18
Manim's method 17
Manim's method 5
Manim's method 3
Manim's method 15
Manim's method 11
Manim's method 20
Binary method
Manim's method 10
Manim's method 9
Manim's method 6
Manim's method 21
Manim's method 8
Manim's method 14
Manim's method 19
Understand k-means method
Clustering of clustering method
Manim's method part 23
Dictionary items method
[PyTorch] Installation method
N cross method
Image collection method
Regression analysis method
Gradient method implementation 1
Python-peewee connection method
Class method static method
youtube-dl update method
Monte Carlo method
Mode-Matching Method Simulation_Python
Johnson method (python)
[Python] Semi-Lagrange method