我正在努力学习蟒蛇。我正在编写一个小程序,它在静态数组中保存一些数字或字符串。我希望我的函数将变量保存在单个数组中。但是当我完成我的函数后,数组也消失了。如何在python中使我的数组成为静态数组?我想在几个函数中对其进行更改。
py_ppl = []
def Dong():
alc1 = alc.get()
alc2 = alc1
alc1 = [0]
py_ppl.append(alc1[0])
py_ppl.append(alc2)
我的意思是像这样的东西。我和Tkinter Gui一起得到了alc。
发布于 2018-06-03 08:48:15
这个使用类变量的示例可能会对您有所帮助。在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
打印
['a', 'b']
['a', 'b']
https://stackoverflow.com/questions/50662164
复制相似问题