Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
多语句的Python循环_Python_Declare - Fatal编程技术网

多语句的Python循环

多语句的Python循环,python,declare,Python,Declare,python中的简单声明类似于 x = 1 y = 1 z = 1 让它更容易 x,y,z = [1] * 3 但是对于函数“语句” 为什么我只能对多重声明使用循环? e、 g 这个怎么样 menus = [ (self.exc11, exc11id), (self.exc15, exc15id), (self.exc37, exc37id), (self.exc55, exc55id), (self.exc88, exc88id), (sel

python中的简单声明类似于

x = 1
y = 1
z = 1
让它更容易

x,y,z = [1] * 3
但是对于函数“语句”

为什么我只能对多重声明使用循环? e、 g

这个怎么样

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_)
这个怎么样

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

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

例如,而不是变量

exc11id exc15id exc37id exc55id exc88id exc99id

您可以使用dict:

excid = {'11': ..., '15': ..., }
类似地,您可以使用单个属性
self.exc
,而不是许多属性,这是一个dict:

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)
不起作用是因为
x
xid
引用的值是
str
s。编写
self.x
时,Python正在查找名为
x
的属性,而不是名为变量
x
引用的值的属性。 相反,是您正在寻找的函数调用

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


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

例如,而不是变量

exc11id exc15id exc37id exc55id exc88id exc99id

您可以使用dict:

excid = {'11': ..., '15': ..., }
类似地,您可以使用单个属性
self.exc
,而不是许多属性,这是一个dict:

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)
不起作用是因为
x
xid
引用的值是
str
s。编写
self.x
时,Python正在查找名为
x
的属性,而不是名为变量
x
引用的值的属性。 相反,是您正在寻找的函数调用


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

关于stackoverflow的第一篇文章很多,让人惊讶的是我能得到答案的速度。关于stackoverflow的第一篇文章很多,让人惊讶的是我能得到答案的速度。对不起。但我想在循环中声明我的方法是浪费“行”对不起。但我想在循环中做所有的陈述我的方法是浪费“行”