这个使用类变量的示例可能会对您有所帮助。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']