[PYTHON] Class variables

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

Class variables
Class variables
class
class
Python: Class and instance variables
Globalization of class instance variables
Python class variables and instance variables
Landmines hidden in Python class variables
[Python] Variables
Class inheritance
How should I access class variables?
[Python] Inherit a class with class variables
Abstract class
Example of using class variables and class methods
[Python] class, instance
"Kanrika" python class
About python, class
[python] Difference between variables and self. Variables in class
Go class basics
Page processing class
Shell variables, environment variables
Class method static method
Python class, instance
Proprietary exception class
JSON conversion class
#Python basics (class)