如何在两个类中使用同一个变量

0 投票
1 回答
602 浏览
提问于 2025-04-16 05:59
# I have this class:

class Test(webapp.RequestHandler):
    myList = []
    def get(self):
        s = [self.request.get('sentence')]
        self.myList.append(s)
        htmlcode1 = HTML.table(self.myList)
        myListLen = len(self.myList)
        lastItem = self.myList[myListLen-2]

# I want to add the following to delete the contents of `myList` when I enter 'delete' in the form:

class Delete(webapp.RequestHandler):
    def get(self):
        if s == ['delete']:
            del self.myList[:]

我该如何在 Delete() 这个函数里面使用 self.myList 呢?

1 个回答

6

你应该使用

Test.myList

因为myList是这个类的一个属性,而不是这个类的某个特定实例的属性。

撰写回答