While watching Python's official 2.7 document, try to test some examples to deepen your impressions.
Official Document URL: http://python.usyiyi.cn/python_278/tutorial/classes.html
The code is as follows:
#Coding:utf-8##################Practice Application Classes#################classTest:def __init__(self, name): Self.name=name x= Test (1) PrintX.name#here try to output ' Hello ' ———— change line tenth to: x = Test (u ' hello ')###################classtestvarible:samething='Hello Word.' #Shared Variables def __init__(self, name): Self.name= Name#unique variables for each instance One= Testvarible (' One') the= Testvarible (' Both')Print 'One :', One.name,'; Same thing:', One.samethingPrint 'Both :', Two.name,'; Same thing:', Two.samething###################classTesterror:tricks= []#The right thing to do is to move this sentence in the __INIT__ function or in the Add_tricks function .#def __init__ (self):# defadd_tricks (Self,stuff): Self.tricks.append (stuff) Dog1=Testerror () dog1.add_tricks ('Dog1') dog2=Testerror () dog2.add_tricks ('dog2')PrintDog1.tricks,'\ n----------'PrintDog2.tricks#after the output you will find that tricks is a shared list and the object's data becomes shared.#It is important to note that the attribute name and the method name in the class must be distinguished from each other, and there will be a hard to find error.#declaring a naming convention again can avoid a lot of hassle. ####################
Beginner Python class