Tchaikov’s Journal

July 5, 2006

Python 的点点滴滴——?:

Filed under: Python

Python 可以用 [false_exp, true_exp][bool_exp] 的方式模拟 C 语言里的a?b:c三目操作符。对于那些不在乎没有被取值,但是也同样被 evaluate 的 {true|false}_exp 来说,是相当合适的。

但是今天碰上了一件怪事,是在运行那个用 wxPython 写的演示程序时,Python2.3 说:

return (val/2, [0,1][val > self.upper or val < self.lower])
TypeError: list indices must be integers

开始的时候打开 ipython (Python2.4)又试了一下,好的啊。再用 Python2.3 试,也没问题。晕菜了,把[0,1][val > self.upper or val < self.lower]改成[0,1][int(val > self.upper or val < self.lower)]。终于闭嘴了,清静了。不过还是不知道他为什么会这样。

Comments »

The URI to TrackBack this entry is: http://tchaikov.blogsome.com/2006/07/05/p42/trackback/

No comments yet.

RSS feed for comments on this post.

Leave a comment

Line and paragraph breaks automatic, e-mail address never displayed, HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>


Get free blog up and running in minutes with Blogsome
Theme designed by Jay of onefinejay.com