class T(object):
#Class variables (shared by all objects
words = []
def add_word(self,word):
#Note that you add self when accessing class variables
self.words.append(word)
c = T()
c.add_word('add 1')
c.add_word('add 2')
print(c.words)#['add 1', 'add 2']
d = T()
d.add_word('add 3')
d.add_word('add 4')
print(d.words) #['add 1', 'add 2', 'add 3', 'add 4']
Recommended Posts