2016-04-27 19 views
0

if文の外のリストにアクセスしたいのですが、Pythonに静的関数がありますか?if文の外側でリストからアクセスできるようにするには?

一覧switch1Flowswitch2Flowswitch3Flow、次のとおりです。ここで

コード

def startingNode(self, destID, message, counter): 
    pack = self.pac.packet(self.name, destID, message) 
    if counter == "0": 
     nextHop = self.controller.selectSwitch(self.name) 
     print "Switch", self.name, "contacts controller" 
     nextHop.receivePacket(pack) 
     switch1Flow = [nextHop.name] //list1 
     print switch1Flow 
     nextHop1 = self.controller.selectSwitch(nextHop.name) 
     print "Switch", nextHop.name, "contacts controller" 
     nextHop1.receivePacket(pack) 
     nextHop2 = self.controller.selectSwitch(nextHop1.name) 
     print "Switch",nextHop1.name,"contacts controller" 
     nextHop2.receivePacket(pack) 
     switch2Flow = [self.name, nextHop1.name] // list2 
     print switch2Flow 
     switch3Flow = [nextHop.name, nextHop2.name] // list3 
     print switch3Flow 
    elif counter == "1": 
     if 's2' in switch1Flow: 
      print "forwarding to s2" 
      if 's1' in switch2Flow and 's3' in switch2Flow: 
      print "forwarding to s3" 
      if 's2' in switch3Flow and 's4' in switch3Flow: 
       print "forwarding to s4" 
       print "message reched s4" 

私はどのようにあなたが私を提案してくださいelif.Canにアクセスできるようにするswitch1Flow、switch2Flowとswitch3Flowにアクセスしたいです?

+1

してください[編集](http://stackoverflow.com/posts/36894439/edit)あなたのポストをそれはいくつかの理にかなっているように、 。 –

+0

'if'で初期化されていない' elif'の変数の値はどうでしょうか? –

+0

実際には、カウンターがゼロより大きいときはいつでも、リストを使用可能にしてチェックする必要があります。 – pythonbeginer

答えて

1

あなたは大丈夫でなければならif else条件外のスイッチ変数を定義します。

def startingNode(self,destID,message,counter): 
    pack = self.pac.packet(self.name,destID,message) 
    switch1Flow = [] 
    switch2Flow = [] 
    switch3Flow = [] 
    if counter == "0": 
     nextHop = self.controller.selectSwitch(self.name) 
     print "Switch",self.name,"contacts controller" 
     nextHop.receivePacket(pack) 
     switch1Flow = [nextHop.name] //list1 
     print switch1Flow 
     nextHop1 = self.controller.selectSwitch(nextHop.name) 
     print "Switch",nextHop.name,"contacts controller" 
     nextHop1.receivePacket(pack) 
     nextHop2 = self.controller.selectSwitch(nextHop1.name) 
     print "Switch",nextHop1.name,"contacts controller" 
     nextHop2.receivePacket(pack) 
     switch2Flow = [self.name,nextHop1.name] //list2 
     print switch2Flow 
     switch3Flow = [nextHop.name,nextHop2.name] //list3 
     print switch3Flow 
    elif counter == "1": 
     if 's2' in switch1Flow: 
      print "forwarding to s2" 
      if 's1' in switch2Flow and 's3' in switch2Flow: 
      print "forwarding to s3" 
      if 's2' in switch3Flow and 's4' in switch3Flow: 
       print "forwarding to s4" 
       print "message reched s4" 
関連する問題