不可用类型:在mininet中运行python脚本时出现'list'错误

问题描述 投票:-2回答:1
#!/usr/bin/python

from mininet.topo import Topo
from mininet.net import Mininet
from mininet.link import Link
from mininet.log import setLogLevel, info
from mininet.cli import CLI

class myNetwork(Topo):
 "three users connected to cloud"

 def build(self, **_opts):
  #create switch
  s1 = \
  [ self.addSwitch for s in 's1' ]
  #create hosts
  h1, h2, h3 = \
  [ self.addHosts for h in 'h1', 'h2', 'h3' ]
  #create links
  [for h,s in [(h1,s1), (h2,s1), (h3,s1)]: self.addLink(h,s)

def run():  
 topo = myNetwork()
 net = Mininet(topo=topo)
 net.start()
 CLI(net)
 net.stop()

if __name__=='__main__':
setLogLevel('info')
run()

当我尝试在mininet中运行我的python脚本时,我收到错误:

不可用类型:'列表'

我试图研究这个,我理解错误意味着什么,但我不确定为什么我运行我的python脚本时出现这个错误。

python network-programming mininet
1个回答
1
投票

看起来你对List Comprehensions的工作方式有点困惑。你可能想要做

[ self.addSwitch for s in s1 ] 
# notice the lack of quotes around s1, this means we want the VAR not the string

代替

[ self.addSwitch for s in 's1' ]

此外,您可能忘记从此行中删除[,因为我认为这不是正确的语法:

[for h,s in [(h1,s1), (h2,s1), (h3,s1)]: self.addLink(h,s)

至:

for h,s in [(h1,s1), (h2,s1), (h3,s1)]: self.addLink(h,s)

以下是列表理解的几个示例:

l = [1, 2, 3, 4]

l_modified = [i+1 for i in l] # [2, 3, 4, 5]

a = "String"
a_list = [c for c in a] # ["S", "t", "r", "i", "n", "g"]

b = [c.upper() for c in "hello"] # ["H", "E", "L", "L", "O"]
© www.soinside.com 2019 - 2024. All rights reserved.