Examples of simple analysis: classes and instances in Python

Why class?

Python provides many simple data structures such as lists and strings, more than enough to deal with most of the demand, but in some cases, just use these simple data structures can not express what we want to achieve, such as a car, or some of the more refined, Audi Q7 or a Tesla electric car.

You can specify a list, the first element at car brand, the second element of the model car store, the third element storage car factory time, but how do you know a certain element corresponds to what specific information about the car?

This approach is too subjective, arbitrary, nor intuitive. You might think of using the dictionary of key-value pairs expressed, so that means we have to re-enter a lot of code. For more intuitive to solve such problems, we introduce the concept of class.

Class allows us to create complex data structures, and any use of any useful information. For example, we create a representation car classes, this class has three attributes: brand, model, date of manufacture.

Examples of how to define classes and class?

Next, we distinguish between "instance of the class" two key words "class" and. Determining a class structure, but the content is not filled. For example, it indicates the car class, indicating each car needs to keep three: Brand, model number, and date of manufacture, but did not state the specific car brand, model or date of manufacture.

The instance of the class is a special class: identifies the specific information corresponding to various attributes, for example, I created a car, the Audi brand, model Q7, the time the factory is 2017. To better understand and distinguish between "instance of the class," "class," and our analogy, we all filled out personal information registration table, everyone must fill in the form corresponding information, such as name, gender, age and so on, but the specific contents of this form varies.

This class is equivalent to a blank registration form, specified above to fill in the content. And after you fill out the table, the equivalent of an instance of the class, above the recorded information related to your personal. This creates an instance of the class based on a process called "instantiation."

Well, now we look at the real Python class (Figure I car.py). The first word in class means that we create a class. Car second word is the name of the class, followed by a pair of parentheses, generally speaking, the default class name capitalized. Class variable called attributes, class function is called a method.

__init __ () is a special initialization function when creating Car instance, automatically call the method, although not explicitly contain a return statement, but automatically returns a Python instance represents the car, for example, my_car = Car ( 'Audi', 'Q7', '2017') will assign Audi self.make, assigned to Q7 self.model, 2017 assigned to the self.year, and returns an instance my_car. To self prefixed variables are available to all methods in this class use, for example, in get_descriptive_name (), you can call self.make, self.model, self.year.

The self can be understood as an instance of a class, more precisely, is a reference to the instance itself, so that instances can access the properties and methods class. Remember the parable about the registration form before it? Each instance has the same field structure, but different for each field to fill in the information, which is why write self.make = make, instead of writing Car.make = make.

You may notice that the class of each method first parameter is the keyword self, self is essential, and must be the first, but when we call these methods when (eg my_car = Car ( ' Audi ',' Q7 ',' 2017 ')), only need to pass three values, why skip this parameter it self?

This is the Python-specific behavior: when we call an instance method, Python will automatically figure out which one to use instance and pass it to the self parameter. When we introduce a method on how to use the next class, give you a detailed explanation.

Figure I car.py

How to use the class?

Python using dot access object properties and methods: my_car.year, my_car.get_descriptive_name (),

Figure II my_car.py code line 7-8.

Back to the previous question, drawing a line in the ninth custom code when get_descriptive_name (self) parameters essential self, why Figure II line 8:00 code calls this method, omit the argument self?

With dot indicates examples my_car precisely because we call get_descriptive_name () when, Python can automatically identify instances my_car, and create a reference my_car and reference pass this parameter self. If the instance method call is not passed, we would look like this:. Car get_descriptive_name (my_car), at this time, clear the my_car as arguments passed to the parameter self, two lines of code shown in Figure 9.

Figure II my_car.py

Figure III is a result of the program:

Figure III operating results

In this section we learned instance of class and class, and how to use the class.

Guess you like

Origin blog.csdn.net/sinat_38682860/article/details/94762980