動作環境
ideone (Python 3.5)
pi1 = 3.1415
pi2 = 3.1415
if (pi1==pi2>1):
print('larger than 1 and equal')
run
larger than 1 and equal
PyMieScattのコードで上記のような==
と>
のチェックをしている実装がある。
「pi1とpi2が同じ値でかつ、1より大きい」という実装のようだ。
こういう書き方は読みやすいのだろうか? (違和感を感じた)
link
複数の<
を使えるという記載は以下に見つけた。
x < y < z
という書き方の用語名は不明。
Also note that the semicolon binds tighter than the colon in this context, so that in the following example, either all or none of the print() calls are executed:
if x < y < z: print(x); print(y); print(z)
違和感の正体
違和感の正体に気づいた。
Cでの実装を見る。
#include <stdio.h>
int main(void) {
int pi1 = 314;
int pi2 = 314;
int wrk;
wrk = (pi1 == pi2);
printf("%d\n", wrk); // 1
if (pi1 == pi2 > 1) {
printf("larger than 1 and equal\n");
}
return 0;
}
run
1
larger than 1 and equal
は表示されない。
Pythonでは問題ない実装は、Cでは問題がある実装となる。
CもPythonも(+他の言語も)使う人にとっては違和感のある書き方なのだろう。