9. Object-Oriented - Classes and Objects

1. Object

Refers to the combination of characteristics and skills

Advantages of object-oriented programming: strong scalability

Disadvantage: programming complexity is higher than procedure-oriented

 

Second, the class

A class is a collection of objects that combine the same characteristics and skills

In the program, the class must be defined first, and then the class must be called to generate the object

 

Syntax to define a class:

class class name: the class name is in camel case, the first letter is capitalized

 

Use variables to represent features and functions to represent skills

The most common in the class is the definition of output variables and functions, but there can be any python code in the class

 

In the definition phase, the code in the class body will be executed immediately, and then the [generated name] will be stored in the class namespace. Use classname.__dict__ to view namespaces

 

When using a class, use . , similar to a module, in front of . is the namespace, and the one after . is called [property]

 

The object of the class

The object consists of the arguments enclosed in parentheses when the class is called

Class objects support two operations: property reference and instantiation

 

Fourth, the method of the class

Inside a class, use the def keyword to define a method. Unlike general function definitions, class methods must contain the parameter self, which is the first parameter. When self represents an instance of a class, not a class

 

Five, __init__ method

Define an __init__ function within the class, which will automatically trigger execution when the class is called

Function: used to initialize unique attributes for the class in the class call phase
1. There can be any python code in this method

2. There must be no return value

 

6. Calling a class - instantiation of a class

Use () to call a class, [generate the object of the class], the object can also be called an instance of the class, and the process of calling a class is also called instantiation of the class

 

It happens when the class is called (instantiated):

1. First, an empty object will be generated

2. The __init__ function inside the class will be automatically triggered

3. Then pass the empty object and the parameters passed in the call class brackets to the __init__ function to customize the unique attributes for the object

 

Whenever a function is defined inside a class, it will automatically pass in a self parameter. When the object is called, it will automatically pass itself in.

 

self needs to be defined when it is defined, but it is automatically passed in when it is called

The name of self is not meant to be dead, but it is better to use self by convention

self always refers to the instance of the class on call

 

7. Object operation

View namespace with classname.__dict__

classname.property view

classname.attribute = added content added

classname.attribute = modified content modified

del classname.attribute delete

 

Eight, the search order of object properties

Find the object's own namespace first - then the class's namespace

 

The data attribute of the class is for the object, and it is directly shared with all objects, and the memory address is the same

The function attribute of the class is also used for the object, but it is used for the binding object. Binding to different objects is a different binding method, and the memory addresses are different, but in fact they are all the same function

 

9. Binding method

The binding method means that the function property of the class is bound to the object for use

 

The special feature of the binding method is that it should be called by whoever it is bound to; whoever is called will be automatically passed in as the first parameter.

 

The function defined inside the class can be used by the class, but when the class is used, it is an ordinary function. The ordinary function has several parameters and passes in several parameters.

The functions defined inside the class are actually used by objects and bound to objects. Binding to different objects is a different binding method.

The variables defined inside the class are shared by all objects, and all objects point to the same memory address

 

10. Everything is an object

Class and type are a concept

 

11. Inheritance

Inheritance is a way to create a new class, the new class is called a subclass or a derived class, and the parent class can be a base class or a superclass

Supporting a subclass in python can inherit multiple parent classes, and the subclass will inherit the attributes of the parent class

 

The benefit of inheritance: reducing code redundancy

 

In python3, if a class does not specify an inherited parent class, it inherits object by default

 

In the context of diamond inheritance, look for properties:

Classic Class: Depth First

New-style classes: breadth-first

 

Find inheritance relationship: Inheritance is the relationship between classes and classes. To find this relationship, you need to abstract first and then inherit

 

12. Derivatives

The subclass defines its own new properties. If it has the same name as the parent class, the subclass's own shall prevail.

 

13. Reuse the function of the parent class in the new method derived from the subclass, also called method rewriting

Method 1: Call by name (in fact, it has nothing to do with inheritance), you need to pass in self

Method 2: super() call (strictly depends on inheritance)

The return value of super() is a special object, which is specially used to call the properties in the parent class without passing self

super().__init__()

 

14. Combination

There are two solutions to the problem of code redundancy between classes: 1. Inheritance 2. Composition

Compared with inheritance, the decoupling of composition is higher, so use composition if you can use composition

 

Inheritance describes what is the relationship between classes and classes

Composition describes the relationship between classes, what kind of relationship

 

15. Packaging

Encapsulation is to store a bunch of attributes and then hide this attribute

The purpose of encapsulation: to isolate complexity. A clear distinction between inside and outside is hidden from the outside and open to the inside. Encapsulate data attributes, and then need to open up interfaces for classes to use

 

Add __ at the beginning of the attribute to hide the attribute (be careful not to add __ at the end)

 

important point:

1. In fact, this kind of hiding is just a grammatical deformation, not external but internal

2. This grammatical deformation occurs only once in the class definition stage. After the class is defined, the newly added attributes at the beginning of _ have no deformation effect.

3. If the parent class does not want the subclass to override its own method, you can add __ before the method name

 

Happened:
The attribute name was uniformly deformed in the class definition phase: _ own class name __ attribute name

 

property module

Used to disguise a function within a class as a data attribute

 

16. Polymorphism

Polymorphism refers to the fact that the same thing has multiple forms, and inheritance is used to represent affiliation in the program

Polymorphism: You can use the methods under the object without considering the specific type of the object

 

abc module

 

17. Binding method and non-binding method

@classmethod: bind method, bind a function in the class to the class

@staticmethod: Unbound method, unbinds a function in the class

 

18. Reflection

Manipulating properties of classes and objects through strings is called reflection

 

built-in function

isinstance() determines the type of an object

issubclass() Determines whether a class is a subclass of a class

 

Functions dedicated to manipulating class and object properties

hasattr() judgment

getattr() get

setattr() set

delattr() delete

 

Nineteen, class built-in proprietary methods

__init__ constructor, called when the object is generated

__del__ destructor, used when the object is released, automatically executed when the object is deleted

__repr__ print, convert

__setitem__ is assigned by index

__getitem__ gets the value by index

__len__ gets the length

__cmp__ comparison operation

__call__ function call

__add__ addition operation

__sub__ Subtraction

__mul__ Multiplication

__div__ division operation

__mod__ remainder operation

__pow__ power

__class__ which class is used to get the current instance object

__name__ 1) When the file is called, the value of __name__ is the module name; 2) When the file is executed, the value of __name__ is '__main__'

 

20. Metaclasses

exec() is used to process the code in the string

A class of a class is a metaclass

Classes defined with class are used to generate our own objects

The built-in metaclass type is a class used to specifically generate class definitions

Use the built-in metaclass type() to instantiate our class

 

type (class name, base class of class, dictionary of class)

 

Custom metaclass: 1. To control the creation 2. To control the calling process of the class (the process of instantiation)

 

Twenty-one, singleton mode

A singleton means that an instance of a class can only be created once from beginning to end. The main purpose of the singleton pattern is to ensure that only one instance of a class exists

 

Methods to implement the singleton pattern:
1. Use the __new__ method

2. Use decorators

3. Use metaclasses

4. Use modules: Python modules are natural singleton patterns. You only need to define related functions and data in a module to get a singleton object.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324807681&siteId=291194637