簡體   English   中英

Python Tornado:實例變量不會從方法保存到另一個方法

[英]Python Tornado : Instance variable are not saved from a method to another

我在Tornado中為實例變量苦苦掙扎了一下。 我的代碼如下:

def initialize(self):
    self.needed_file = []

def put(self):
    try:
        if something:
            self.needed_file.append('Ninja file')
            self.needed_file.append(dico_data[0][1].decode('utf-8'))
            print(self.needed_file)
    except IndexError:
        pass   

def get(self):
    try:
        print('//' + str(self.needed_file) + '//')
    except AttributeError:
        print('{}'.format(ServerHandler.needed_file))

我沒有設法將self.needed_file的值從put(self)打印to get(self) ,解釋器給了我這種追溯。

['Ninja file', '/Users/corpo/DistributedBuild/]` <----------- print() from put
[I 180503 10:01:59 web:2106] 200 PUT / (::1) 11.87ms
//[]// <--------- print() from get
[E 180503 10:01:59 web:1621] Uncaught exception GET / (192.168.1.189)

當然,未捕獲的表達式是“索引錯誤異常”,因為我要讓我給它提供一個它沒有的列表中的值。

謝謝 !

將實例變量定義為類的一部分

class Test():

    needed_file = []

    def put(self):
        print("put")
        self.needed_file.append('Ninja file')

    def get(self):
        print("get")
        print(self.needed_file)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM