Studied python for a week. Analyze some of the syntax in Python with an example of a simple class inheritance.
1 classAnimal:2Age = 13Name ='Luo'4 def __init__(self, N, a):5Self.name =N6Self.age =a7 Print("parent class, name:%s, age:%d"%(Self.name, self.age))8 9 classCat (Animal):Tencolor ="' One def __init__(self, A, n, c): AAnimal.__init__(self, A, n) -Self.color =C - ifc = ='White': the Print("color of the cat initialized white") - Else: - Print("Cat not initialized white") - Print("subclass, Name:%s, Age:%d, color:%s"%(Self.name, Self.age, Self.color)) + - if __name__=='__main__': +var = Cat ("Small white", 4,'White')
1 The class in Python is defined in the class name:
2 How is a colon usually used? A colon is generally used in the definition of a class, the definition of a function, condition judgment, and conditional control. For example, the use of the If else in 14-17 lines of code.
3 Python does not use {} to identify blocks of code, using indentation to represent the relationship between the code, such as the following code:
while in Rang (1,10) print(i)//Here is indented with the TAB key, for a demo, if you indent with tab, then all with tab, with spaces, all with a space indentation.
Class 4 inheritance, write the parent class in parentheses, representing the inheritance relationship, as shown in the 9th line of code.
The understanding of constructors in Class 5. def __init__ (self, N, a): function name (followed by double underscore) __init__, the first argument self, represents an entity object reference for the class.
6 20, 21 lines of code generally represent the entry of a. py file.
7 The end of each statement in Python does not have to be well-divided
8 Python does not have to define the type of the variable, he is assigned to what type, such as var = (All-in-one) means that Var is a tuple.
Python class analysis (including simple syntax analysis)