[PYTHON] Special method

python


class Word(object):
    def __init__(self, text):
        self.text = text
    
    def __str__(self):
        return 'texttxet'
    
    def __len__(self):
        return len(self.text)
    
    def __add__(self, word):
        return self.text.lower() + word.text.lower()
    
    def __eq__(self, word):
        return self.text.lower == word.text.lower()

w = Word('aaaaaaaaaa')
w2 = Word('bbbbbbbbbbbbb')

print(w)
print(len(w))
print(w + w2)
print(w == w2)

Execution result


texttxet
10
aaaaaaaaaabbbbbbbbbbbbb
False

Recommended Posts

Special method
Special method
Manim's method 13
Manim's method 2
Manim's method 17
Manim's method 5
Manim's method 3
Manim's method 11
Manim's method 16
Manim's method 20
Binary method
Manim's method 10
Manim's method 6
Manim's method 21
Manim's method 4
Manim's method 8
Manim's method 14
Manim's method 22
Manim's method 19
Manim's method 12
Understand k-means method
Clustering of clustering method
Manim's method part 23
Dictionary items 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
Johnson method (python)
[Python] Semi-Lagrange method