试图执行某人的代码,得到一个语法错误。不知道为什么:(
def GetParsers( self, systags ):
childparsers = reduce( lambda a,b : a+b, [[]] + [ plugin.GetParsers( systags ) for plugin in self.plugins ] )
parsers = [ p for plist in [ self.parsers[t] for t in systags if self.parsers.has_key(t) ] for p in plist ]
return reduce( lambda a,b : ( a+[b] if not b in a else a ), [[]] + parsers + childparsers )
错误是
File "base.py", line 100
return reduce( lambda a,b : ( a+[b] if not b in a else a ), [[]] + parsers + childparsers )
Python版本
Python 2.2.3 (#1, May 1 2006, 12:33:49)
[GCC 3.2.3 20030502 (Red Hat Linux 3.2.3-54)] on linux2
^
发布于 2010-08-02 06:48:47
条件表达式是在2.5 (source)中添加的-您有2.2版本。所以我担心你没有条件表达式。它们只是在那个版本中还不存在。如果可以的话,一定要更新(不只是这个小小的改变,从06年以来已经有上千个了)。
发布于 2010-08-02 06:51:35
您需要将Python安装升级到至少2.5。More Information
发布于 2010-08-02 08:38:43
升级到较新版本的Python将是最好的解决方案,但如果由于某些原因无法升级,您可以将代码更新为使用the and-or trick。
所以这就是:
>>> 'a' if 1 == 2 else 'b'
'b'
变成:
>>> (1 == 2) and 'a' or 'b'
'b'
这里有一个小问题,如果你为True返回的值本身计算为False,那么这个语句就不会像你想要的那样工作。您可以按如下方式解决此问题:
>>> ((1 == 2) and ['a'] or ['b'])[0]
'b'
在这种情况下,因为值是一个非空列表,所以它永远不会计算为False,所以这个技巧总是有效的。
https://stackoverflow.com/questions/3385539
复制