Python ทำสิ่งพิเศษด้วยการเปรียบเทียบแบบล่ามโซ่
สิ่งต่อไปนี้ได้รับการประเมินแตกต่างกัน:
x > y > z # in this case, if x > y evaluates to true, then
# the value of y is being used to compare, again,
# to z
(x > y) > z # the parenth form, on the other hand, will first
# evaluate x > y. And, compare the evaluated result
# with z, which can be "True > z" or "False > z"
ในทั้งสองกรณีหากเป็นการเปรียบเทียบครั้งแรกFalse
ส่วนที่เหลือของข้อความจะไม่ถูกพิจารณา
สำหรับกรณีเฉพาะของคุณ
1 in [] in 'a' # this is false because 1 is not in []
(1 in []) in a # this gives an error because we are
# essentially doing this: False in 'a'
1 in ([] in 'a') # this fails because you cannot do
# [] in 'a'
นอกจากนี้เพื่อแสดงให้เห็นกฎข้อแรกข้างต้นนี่คือข้อความที่ประเมินเป็น True
1 in [1,2] in [4,[1,2]] # But "1 in [4,[1,2]]" is False
2 < 4 > 1 # and note "2 < 1" is also not true
ลำดับความสำคัญของตัวดำเนินการ python: http://docs.python.org/reference/expressions.html#summary
if a < b < c:
และทำให้มันทำงานได้โดยสัญชาตญาณ