Some object oriented concepts from the python foundation tutorial

  • 2020-04-02 14:03:34
  • OfStack

Python USES classes and objects for object-oriented programming (OOP).

The primary goal of object orientation is to improve program reusability. The reason we got into object-oriented programming so early is that the whole concept of Python is object-based. Understanding OOP is the key to further learning Python.

The following is an understanding of object orientation, based on classification.

Similar objects, classified as classes

In human cognition, we classify things according to their similar attributes and give them names. For example, the common attribute of birds is that they have feathers, which are produced by laying eggs. Any particular bird is based on the archetype of a bird.

Object orientation simulates the above human cognitive processes. In Python, to sound cool, we call the above "thing" an object.

Let's define birds


class Bird(object):
    have_feather = True
    way_of_reproduction  = 'egg'

We define a class, which is a Bird. In the statement block that belongs to this analogy, we define two variables, have_feather and way_of_reproduction, which correspond to the attributes we just talked about. We won't go into the parentheses and what's in them for the moment, but we'll call that problem number one.

Let's say I have a chicken named summer. It's an object, and it belongs to birds. Use the previously defined classes:


summer = Bird()
print summer.way_of_reproduction

By creating an object in the first sentence and explaining that summer is an object in the category bird, summer has the class attribute of the bird, and the reference to the attribute is realized in the form of object.attribute.

Poor summer, you're a frill egg.

action

In everyday cognition, when we identify categories by attributes, we sometimes distinguish categories by what this thing can do. For example, birds move. In this way, birds are distinguished from houses. These actions lead to certain results, such as movement leading to a change in position.

Some of these "behavior" attributes are methods. Python illustrates methods by defining functions within a class.


class Bird(object):
    have_feather = True
    way_of_reproduction = 'egg'
    def move(self, dx, dy):
        position = [0,0]
        position[0] = position[0] + dx
        position[1] = position[1] + dy
        return position summer = Bird()
print 'after move:',summer.move(5,8)

We've redefined the category of birds. The bird has added a method property, which is the move method. (I admit this is a silly method, but you can define a more interesting method after you see the next lecture.)

(it takes a self as an argument, so that we can refer to the object itself. The first argument to the method must be self, whether it is used or not. I'm going to expand on self in the next video.)

The other two parameters, dx and dy, represent the distance traveled in the x and y directions. The move method will eventually return the computed position.

When we finally call the move method, we only pass the dx and dy arguments, so we don't need to pass the self argument (because self is just for internal use).

My summer can run.

A subclass

The category itself can be further subdivided into subclasses

For example, birds can be further divided into chickens, geese and orioles.

In OOP, we express this concept through inheritance.


class Chicken(Bird):
    way_of_move = 'walk'
    possible_in_KFC = True class Oriole(Bird):
    way_of_move = 'fly'
    possible_in_KFC = False summer = Chicken()
print summer.have_feather
print summer.move(5,8)

Possible possible le_in_kfc for the newly defined Chicken class with two properties: way_of_move

When the class is defined, the parenthesis is Bird. This means that Chicken is a subclass of Bird. Naturally, Bird is the parent of Chicken. Chicken will enjoy all the attributes of Bird. Although I only declared that summer is a chicken, it enjoys the attributes of its parent class through inheritance (whether the variable attribute have_feather or the method attribute move)

The newly defined Oriole class, also inherited from birds. When an oriole object is created, it automatically has bird attributes.

Through inheritance, we can reduce the repetitive information and repetitive statements in the program. If we define two classes separately and do not inherit from birds, we must enter the attributes of birds into the definition of chicken and oriole respectively. The whole process can get tedious, so object orientation makes the program more reusable.

(back to question 1, object in parenthesis. When the parenthesis is object, the class has no parent.)

We've been practicing this cognitive process of categorizing all kinds of things to understand the world since our ancestors. Process-oriented, where you execute one statement and then the next, is more about machine thinking. With object-oriented programming, we can more easily express the complex ideas in our mind.

conclusion

Categorize things by attributes (classify object as class)

A method is an attribute that represents an action

Use inheritance to illustrate the superclass-subclass relationship. Subclasses automatically have all the attributes of the parent class.

Self represents the object created based on the class definition.

Set up on an object: object name = class name ()

Attributes that refer to objects: object.attribute


Related articles: