作者putumaxally (Puma)
看板Ruby
标题[问题] "p(true and true)" SyntaxError
时间Sat Jan 12 09:25:33 2013
状况:
想请问各位大大为什麽
p(true and true)
会显示语法错误
p (true and true)
p((true and true))
就不会有错误
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 140.118.230.226
1F:推 mars90226:我猜测是and的执行优先顺序很低,换成&&就OK 01/12 12:10
2F:推 zero00072:and 好像被列入不建议使用的名单,就像楼上说的用 &&。 01/12 18:09
3F:→ bassann:同1楼 在这里出错因为程式码想先执行其他东西: 'p(true' 01/12 19:32
4F:→ bassann:所以产生syntax error,而你写的第二、三行是做一样的事情 01/12 19:33
5F:→ bassann:例如 c = a and b; 效果如同 c = a; b; 01/12 19:35
7F:→ putumaxally:那可以再问一下为什麽p(not false)也会错误吗 01/12 19:57
8F:→ putumaxally:not不是control flow operator吧 01/12 20:00
9F:→ bassann:'not' still has a lower precedence(只比or/and高一点) 01/12 20:44
11F:→ bassann:ps. ! is defined as a method and not is an operator 01/12 20:48
12F:→ putumaxally:可是为什麽会发生SyntaxError呢?? 01/12 21:20
13F:→ bassann:因为程式码先evaluate其他部分 而其他部分的程式码syntax 01/13 02:21
14F:→ bassann:不正确(因为切断了) 概念有点像先乘除後加减 01/13 02:22
p(true and true) 我比较能理解为什麽错误
可是 p(not false) 有点难懂...
※ 编辑: putumaxally 来自: 140.118.230.226 (01/13 11:18)
※ 编辑: putumaxally 来自: 140.118.230.226 (01/13 11:19)
15F:→ bassann:是一样的原因... 一个被切在'p(true'另一个是'p(' 01/13 16:41
16F:→ bassann:not和and都是operators不是methods 01/13 16:42
17F:→ bassann:而operators行为像是'等於', methods像是'加减乘除' 01/13 16:46
恩恩,懂了,感谢大大。
※ 编辑: putumaxally 来自: 140.118.230.226 (01/13 17:27)