Python self - Basics point summary (2)

This is the second lesson the basics, see Section:

Python self - Basic knowledge Summary (1)

The first question, What is Python? According to the father of Python Guido van Rossum, then, Python is:

A high-level programming language, its core design philosophy is code readability and syntax that enables programmers with very little code to express their own ideas.
For me, the primary reason is to learn Python, Python is a programming language can be elegant. It can be simple and natural to write code and implement my ideas.
Another reason is that we can use Python in many places: scientific data, Web developers and machine learning and so can use Python to develop. Quora, Pinterest and Spotify use Python to their back-end Web development. So let's learn about Python it.

Class & Objects

Some theories:

Object is a representation of the real world entity, such as a car, a dog or a bicycle. These objects have two main features in common: both data and behavior.
The car has data such as the number of wheels, the number of seats and space of the door, and they can exhibit their behavior: they can accelerate, stop, show how much fuel remaining, as well as many other things.

We will be seen as a data object-oriented programming attributes and behavior. And is expressed as:
data → → properties and behavior methods

The class is a blueprint to create a single object. In the real world, we often find that many of the same types of objects. For example, a car. All cars have the same make and model (have an engine, wheels, doors, etc.). Each vehicle is configured by the same set of blueprints, and have the same components.

Python object-oriented programming model: ON

Python, as an object oriented programming language, there is a concept: classes and objects.

A class is a blueprint, a model of the object.

So, a class is a model, or a way to define the properties and behavior (as we discussed in the theoretical part of that). For example, a vehicle category with its own attributes to define this object is what kind of vehicle. The property has a number of car wheels, energy type, maximum seating capacity and the speed of these.

With that in mind, let's look at the syntax of Python class:

class Vehicle:
    pass

Code above, we use the class statement to define a class. Is not it easy?

Object is an instance of a class, we can be instantiated by the class name.

car = Vehicle()
print(car) # <__main__.Vehicle instance at 0x7fb1de6c2638>

Here, car Vehicle class is an object (or instantiated).

Remember vehicle class has four attributes: the number of wheels, tank type, seating capacity and maximum speed. When we create a new object to be set for all vehicle attributes. So here, we define a class when it accepts parameters initialized:

class Vehicle:
    def __init__(self, number_of_wheels, type_of_tank, seating_capacity, maximum_velocity):
        self.number_of_wheels = number_of_wheels
        self.type_of_tank = type_of_tank
        self.seating_capacity = seating_capacity
        self.maximum_velocity = maximum_velocity

The init method. We call this constructor. So when we create a vehicle object, you can define these properties. Imagine, we like Tesla Model S, so we wanted to create an object of this type. It has four wheels, an electric energy, and the five maximum speed is 250 kilometers (155 miles). We begin to create such an object:

tesla_model_s = Vehicle(4, 'electric', 5, 250)

Four electrical energy + + + five maximum speed of 250 km.

All the properties have been set. But how do we access these property values ​​too? We send a message to the object to the requested value thereto. We call this method. It is the behavior of the object. Let us achieve it:

class Vehicle:
    def __init__(self, number_of_wheels, type_of_tank, seating_capacity, maximum_velocity):
        self.number_of_wheels = number_of_wheels
        self.type_of_tank = type_of_tank
        self.seating_capacity = seating_capacity
        self.maximum_velocity = maximum_velocity    def number_of_wheels(self):
        return self.number_of_wheels    def set_number_of_wheels(self, number):
        self.number_of_wheels = number

This is achieved in two ways number_of_wheels and set_number_of_wheels. We call this getter & setter. Because the first function is getting a property value, the second function is to give the new property value.

In Python, we can use @property (modifier) ​​to define getters and setters. Let's look at the actual code:

class Vehicle:
    def __init__(self, number_of_wheels, type_of_tank, seating_capacity, maximum_velocity):
        self.number_of_wheels = number_of_wheels
        self.type_of_tank = type_of_tank
        self.seating_capacity = seating_capacity
        self.maximum_velocity = maximum_velocity    @property
    def number_of_wheels(self):
        return self.number_of_wheels    @number_of_wheels.setter
    def number_of_wheels(self, number):
        self.number_of_wheels = number

And we can use these methods as properties:

tesla_model_s = Vehicle(4, 'electric', 5, 250)
print(tesla_model_s.number_of_wheels) # 4tesla_model_s.number_of_wheels = 2 # setting number of wheels to 2print(tesla_model_s.number_of_wheels) # 2

This method is defined slightly different. The method here is performed according to the attribute. For example, when we set the number of new tires, we have not seen these two parameters, but the value is set to 2 number_of_wheels. This is one way getter and setter code written in python style.

But we can also be the method used for other things, such as "make_noise" method. let us see:

class Vehicle:
    def __init__(self, number_of_wheels, type_of_tank, seating_capacity, maximum_velocity):
        self.number_of_wheels = number_of_wheels
        self.type_of_tank = type_of_tank
        self.seating_capacity = seating_capacity
        self.maximum_velocity = maximum_velocity    def make_noise(self):
        print('VRUUUUUUUM')

When we call this method, it simply returns a string "VRRRRUUUUM."

tesla_model_s = Vehicle(4, 'electric', 5, 250)
tesla_model_s.make_noise() # VRUUUUUUUM
Published 38 original articles · won praise 1 · views 2210

Guess you like

Origin blog.csdn.net/wulishinian/article/details/102757040