【问题标题】:Python updated list through a classPython 通过一个类更新列表
【发布时间】:2012-10-29 08:44:18
【问题描述】:

我试图创建一个列表,每当调用函数时都会添加到列表中。例如,如果您按下按钮 1,则会调用一个函数,该函数会将 5 添加到 listone,或者如果您按下按钮 2,则会将数字 10 添加到 listone。我希望列表 1 在更新时持续显示结果。 例如我创建一个类 我的类(对象) def 初始化(自我): 听=[] 然后按钮1 按钮2 ..

【问题讨论】:

  • 我们甚至不知道您使用的是什么 gui 框架。我认为您需要向我们展示一些说明您的问题的最少代码

标签: python list class


【解决方案1】:

这样的事情有帮助吗?

class MyClass(object):
    my_list = []
    def __init__(self, initial_list):
        self.my_list = initial_list
    def button_1(self):
        self.my_list = [ele + 5 for ele in self.my_list]
        print(self.my_list)
    def button_2(self):
        self.my_list = [ele + 10 for ele in self.my_list]
        print(self.my_list)

结果:

>>> a = MyClass([1,2,3])
>>> a.button_1()
[6, 7, 8]
>>> a.button_2()
[16, 17, 18]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多