LoginSignup
4
1

More than 5 years have passed since last update.

a == 1 and a == 2 and a == 3 がTrueになるPythonスクリプト

Posted at

QiitaでRuby版の記事を見かけたので,Pythonでも作ってみました。

ソースコード

>>> class Test:
        def __eq__(self, foo):
                return True

>>> a = Test()
>>> if a==1 and a==2 and a==3:
        print('Hello Qiita')

Hello Qiita
>>> 

ポイント(?)

作るにあたって,少し学んだことがあったのでメモしておきます。

==演算子について

Pythonでは,== 演算子は__eq__メソッドで定義されています。もし,定義されていない場合,==isと同じ扱いになります。

>>>class Test2:
        pass

>>> b = Test2()
>>> c = Test2()
>>> d = b
>>> b == c
False
>>> b == b
True
>>> b is b
True
>>> b == d
True
>>> b is d
True

参考

4
1
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
4
1