Python class analysis (including simple syntax analysis)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.