首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用于多语句的Python循环

用于多语句的Python循环
EN

Stack Overflow用户
提问于 2014-07-26 13:03:49
回答 2查看 2.2K关注 0票数 0

在python中的简单声明类似于。

代码语言:javascript
复制
x = 1
y = 1
z = 1

让它更容易..。

代码语言:javascript
复制
x,y,z = [1] * 3

但对于功能“陈述”..。

代码语言:javascript
复制
    import wx
    self.Bind(wx.EVT_MENU, self.exc11, id=exc11id)
    self.Bind(wx.EVT_MENU, self.exc15, id=exc15id)
    self.Bind(wx.EVT_MENU, self.exc37, id=exc37id)
    self.Bind(wx.EVT_MENU, self.exc55, id=exc55id)
    self.Bind(wx.EVT_MENU, self.exc88, id=exc88id)
    self.Bind(wx.EVT_MENU, self.exc99, id=exc99id)
    self.Bind(wx.EVT_MENU, self.rexc11, id=rexc11id)
    self.Bind(wx.EVT_MENU, self.rexc15, id=rexc15id)
    self.Bind(wx.EVT_MENU, self.rexc37, id=rexc37id)
    self.Bind(wx.EVT_MENU, self.rexc55, id=rexc55id)
    self.Bind(wx.EVT_MENU, self.rexc88, id=rexc88id)
    self.Bind(wx.EVT_MENU, self.rexc99, id=rexc99id)
    self.Bind(wx.EVT_MENU, self.excel11, id=excel11id)
    self.Bind(wx.EVT_MENU, self.excel15, id=excel15id)
    self.Bind(wx.EVT_MENU, self.excel37, id=excel37id)
    self.Bind(wx.EVT_MENU, self.excel55, id=excel55id)
    self.Bind(wx.EVT_MENU, self.excel88, id=excel88id)
    self.Bind(wx.EVT_MENU, self.excel99, id=excel99id)
    self.Bind(wx.EVT_MENU, self.calc, id=calcid)
    self.Bind(wx.EVT_MENU, self.edit, id=editid)
    self.Bind(wx.EVT_MENU, self.next, id=nextid)
    self.Bind(wx.EVT_MENU, self.prev, id=previd)

为什么我可以只使用循环来进行多声明?e.g

代码语言:javascript
复制
for x in "exc11 exc15 exc37 exc55 exc88 exc99".split():
    xid = x + "id"
    self.Bind(wx.EVT_MENU, self.x, id=x)
...
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-07-26 13:07:02

代码语言:javascript
复制
ns = vars()
for x in "exc11 exc15 exc37 exc55 exc88 exc99".split():
    xid = x + "id"
    self.Bind(wx.EVT_MENU, getattr(self, x), id=ns[xid])

每当变量名包含数字时,它通常都是一个符号,表明您应该使用列表、元组或迪克,而不是多个变量。如果变量的顺序很重要,请使用列表或元组,否则dict可能更合适。如果值的数目是可变的,请使用列表,否则使用元组。

例如,而不是变量

exc11id exc15id exc37id exc55id exc88id exc99id

你可以用个词:

代码语言:javascript
复制
excid = {'11': ..., '15': ..., }

与此类似,您可以拥有一个属性self.exc,而不是拥有大量属性,这是一个dict:

代码语言:javascript
复制
self.exc = {'11': ..., '15': ..., }

如果进行此更改,则可以将代码简化为

代码语言:javascript
复制
for x in "11 15 37 55 88 99".split():
    self.Bind(wx.EVT_MENU, self.exc[x], id=excid[x])

为什么

代码语言:javascript
复制
for x in "exc11 exc15 exc37 exc55 exc88 exc99".split():
    xid = x + "id"
    self.Bind(wx.EVT_MENU, self.x, id=x)

不起作用是因为xxid引用的值是str的。当您编写self.x时,Python正在寻找一个名为x的属性,而不是一个名称是变量x引用的值的属性。相反,getattr(self, x)是您要寻找的函数调用。

类似地,xid只是一个字符串。要获取名称与该字符串的值相同的变量引用的值,需要在瓦尔斯()全局()命名空间中查找该值。

票数 3
EN

Stack Overflow用户

发布于 2014-07-26 13:06:41

这个怎么样?

代码语言:javascript
复制
menus = [
    (self.exc11, exc11id),
    (self.exc15, exc15id),
    (self.exc37, exc37id),
    (self.exc55, exc55id),
    (self.exc88, exc88id),
    (self.exc99, exc99id),
    (self.rexc11, rexc11id),
    (self.rexc15, rexc15id),
    (self.rexc37, rexc37id),
    (self.rexc55, rexc55id),
    (self.rexc88, rexc88id),
    (self.rexc99, rexc99id),
    (self.excel11, excel11id),
    (self.excel15, excel15id),
    (self.excel37, excel37id),
    (self.excel55, excel55id),
    (self.excel88, excel88id),
    (self.excel99, excel99id),
    (self.calc, calcid),
    (self.edit, editid),
    (self.next, nextid),
    (self.prev, previd),
]

for handler, id_ in menus:
    self.Bind(wx.EVT_MENU, handler, id=id_)
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24971275

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档