Python声明数组/列表static和glob

2024-10-02 16:31:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在努力学习python。我正在做一个小程序,在静态数组中保存一些数字或字符串。我希望我的函数在一个数组中保存变量。但是在我完成我的功能之后,数组也不见了。如何在python中使数组成为静态的?我想在几个函数中改变它。在

py_ppl = []  

def Dong():

    alc1 = alc.get()
    alc2 = alc1
    alc1 = [0]

    py_ppl.append(alc1[0])
    py_ppl.append(alc2)

我是说像这样的。我用Tkinter Gui得到alc。在


Tags: 函数字符串py程序功能def静态数字
1条回答
网友
1楼 · 发布于 2024-10-02 16:31:09

这个使用类变量的示例可能会对您有所帮助。init中声明的变量对于类的每个实例都是局部的,在类顶部声明的变量对于类的所有实例都是全局的。在

class funkBox:
    globalToBox = [] # our class variable
    def __init__(self):
        pass         # our do nothing constructor
    def funk(self,elm): # our function 
        self.globalToBox.append(elm)
    def show(self):     # for demonstration
        print(self.globalToBox)

a = funkBox() #create one instance of your function holder
b = funkBox() #and another
a.funk("a")   #call the function on the first instance
b.funk("b")   # call the function again 
a.show()      # show what instance a has 
b.show()      # and b

印刷品

^{pr2}$

相关问题 更多 >