Class attributes in Python
问题 Is there any difference in the following two pieces of code? If not, is one preferred over the other? Why would we be allowed to create class attributes dynamically? Snippet 1 class Test(object): def setClassAttribute(self): Test.classAttribute = "Class Attribute" Test().setClassAttribute() Snippet 2 class Test(object): classAttribute = "Class Attribute" Test() 回答1: It's more natural to do it like #2, but notice that they do different things. With #2, the class always has the attribute. With