LoginSignup
0
2

More than 3 years have passed since last update.

特殊メソッド

Posted at
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)
実行結果
texttxet
10
aaaaaaaaaabbbbbbbbbbbbb
False
0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2