Ric*_*ard 1 python eval pyjamas
我尝试用变量名定义一个函数.
从数据库中检索名称.对于每个名称,我想定义一个按钮并具有单独的处理:
title=['BNL','CE']
for i in range(0,len(title)):
panelvpu.add(Button(title[i]))
for i in range(0,len(title)):
eval('def onButtonClick'+title[i]+'(self, event):')
eval(' Window.alert("Yes")')
Run Code Online (Sandbox Code Playgroud)
按钮定义没问题,但在定义的函数中处理事件会产生错误
im1 SyntaxError: at index 4 in "def onMenu1Item1(self):
Window.alert("Item 1 selected")": expected ';', got 'onMenu1Item1'
Run Code Online (Sandbox Code Playgroud)
在反馈之后我改变了这个
title=['BNL','CE']
for t in title : panelvpu.add(Button(t))
for t in title:
def_code = "print t"
exec(def_code)
Run Code Online (Sandbox Code Playgroud)
只是为了感受; 在python下这很好用.但我使用睡衣,最后一个代码确实是错误说明
im1 TypeError: iter is undefined
Run Code Online (Sandbox Code Playgroud)
看来睡衣还不支持eval()和exec().
理查德
这里有很多问题:
1)eval用于评估表达式,而不是执行语句.
2)exec需要整个功能在一个exec,而不是分开到单独的行.
3)根据你想要在身体中拥有的东西,有更容易的方法来创建功能.告诉我们你真正想做的事情.
4)你的循环是简单得多:for t in title: blah blah t.