【问题标题】:Python loop for multi statement多语句的Python循环
【发布时间】:2014-07-26 13:03:49
【问题描述】:

python 中的简单声明是这样的。

x = 1
y = 1
z = 1

让它更容易......

x,y,z = [1] * 3

但是对于函数“语句”...

    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)

为什么我可以只使用循环进行多重声明? 例如

for x in "exc11 exc15 exc37 exc55 exc88 exc99".split():
    xid = x + "id"
    self.Bind(wx.EVT_MENU, self.x, id=x)
...

【问题讨论】:

    标签: python declare


    【解决方案1】:
    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

    你可以使用字典:

    excid = {'11': ..., '15': ..., }
    

    同样,您可以拥有一个属性self.exc,而不是拥有很多属性,它是一个字典:

    self.exc = {'11': ..., '15': ..., }
    

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

    for x in "11 15 37 55 88 99".split():
        self.Bind(wx.EVT_MENU, self.exc[x], id=excid[x])
    

    原因

    for x in "exc11 exc15 exc37 exc55 exc88 exc99".split():
        xid = x + "id"
        self.Bind(wx.EVT_MENU, self.x, id=x)
    

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

    同样,xid 只是一个字符串。要获取名称与该字符串的值相同的变量所引用的值,您需要在vars()globals() 命名空间中查找该值。

    【讨论】:

    • 非常感谢 stackoverflow 上的第一篇文章 令人惊讶的是我能以多快的速度得到答案
    【解决方案2】:

    这个怎么样?

    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_)
    

    【讨论】:

    • 对不起。但我想在循环中做出所有声明我的方法是浪费“行”
    猜你喜欢
    • 2017-06-20
    • 1970-01-01
    • 1970-01-01
    • 2018-12-06
    • 2019-11-23
    • 1970-01-01
    • 2013-07-10
    • 1970-01-01
    • 2020-06-18
    相关资源
    最近更新 更多