LoginSignup
0
1

More than 5 years have passed since last update.

Python > if (pi1==pi2>1): > 違和感の正体

Last updated at Posted at 2018-04-02
動作環境
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も(+他の言語も)使う人にとっては違和感のある書き方なのだろう。

0
1
2

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
1