Encapsulation such as Python object oriented programming

  • 2021-12-12 04:39:42
  • OfStack

Directory 1, Encapsulated Understanding 2, Private Class Attributes, Public Class Attributes, Private Instance Attributes and Public Instance Attributes 2.1, Public Class Attributes 2.2, Private Class Attributes 2.3, Public Instance Attributes 2.4, Private Instance Attributes 2.5, Private Attributes Untrue 3, Private Methods and Public Methods 4, Class Reserved Attributes 5, Class Reserved Methods

1. Understanding of packaging

Encapsulation (Encapsulation): Abstraction of properties and methods

Attribute abstraction: Defining, isolating and protecting the attributes (variables) of a class

It is divided into private attributes and public attributes:

Private property: Accessed only within the class Expose properties: can be accessed by class and object name

You can choose to expose or hide attributes, and hide the inherent mechanism of attributes

Method abstraction: Define, isolate and protect the methods (functions) of a class

Divided into private methods and public methods:

Private method: can only be accessed inside the class Expose methods: accessible by class, object name

You can choose to expose or hide the method and hide the internal logic of the method

The goal is to form an interface to externally operable properties and methods

Encapsulation is the process of calling data and code classes, which is expressed as: 类——属性——方法

2. Private Class Attributes, Public Class Attributes, Private Instance Attributes, and Public Instance Attributes

2.1 Exposing class properties

Exposing a class property is a class property, syntax structure


class ClassName:
    < Class attribute name > = < Initial value of class attribute >
    def __init__(self,[ Parameter 1] ,  [ Parameter 2] ,  ...[ Parameter n]):
        self.< Instance attribute name > = < Initial value of instance attribute >
    ...

2.2 Private Class Attributes

Private class properties can only be accessed by the current class, not by subclasses. Grammatical structure


class ClassName:
    <__ Private class attribute name > = < Initial value of private class attribute >
    def __init__(self,[ Parameter 1] ,  [ Parameter 2] ,  ...[ Parameter n]):
        self.< Instance attribute name > = < Initial value of instance attribute >
    ...

.<类属性> Or <对象名>.<类属性> Access in the way of

The controllability of attribute maintenance is effectively guaranteed

The sample code is as follows:


class TestClass:
    __number = 0

    def __init__(self, num_value):
        for i in range(num_value + 1):
            TestClass.__number += i

    @classmethod  #  Class method 
    def sum_number(cls):
        return TestClass.__number


value1 = TestClass(100)
print(TestClass.sum_number())  # 5050
# print(value1.__number)  # AttributeError: 'TestClass' object has no attribute '__number'

In-class take-out visit .__number It will be reported AttributeError Anomaly

2.3 Exposing instance properties

Exposing an instance property is equal to the example property, syntax structure


class < Class name >:
    < Class attribute name > = < Class method value >
    def __init__(self, < Parameter list >):
        self.< Instance attribute name > = < Instance attribute value >
    ...

2.4 Private instance properties

Private instance attributes can only be used inside the current class, nor can subclasses. Grammatical structure


class < Class name >:
    < Class attribute name > = < Class method value >
    def __init__(self, < Parameter list >):
        self.<__ Private instance property name > = < Instance attribute value >
    ...

.<类属性> Or <对象名>.<类属性> Access in the way of

The controllability of attribute maintenance is effectively guaranteed

Sample code:


class TestClass:

    def __init__(self, num_value):
        self.__number = 0
        for i in range(num_value + 1):
            self.__number += i

    def sum_number(self):
        return self.__number


value1 = TestClass(100)
print(value1.sum_number())  # 5050
# print(value1.__number)  # AttributeError: 'TestClass' object has no attribute '__number'

2.5 Private property is not 1. Private property is true

Double underlining of private attributes is only a conversion convention. After conversion, the original name in the class has changed, which is a formal private

Sample code:


class TestClass:

    def __init__(self, num_value):
        self.__number = 0
        for i in range(num_value + 1):
            self.__number += i

    def sum_number(self):
        return self.__number


value1 = TestClass(100)
print(value1.sum_number())  # 5050
print(value1._TestClass__number)  # 5050
#  It can be passed through   Object name ._ Class name __ Attribute   To access the 

It can be accessed by the object name. _ class name __ attribute.

3. Private and public methods

A defined method is a method defined and used within a class. Grammatical structure


class < Class name >:
    def <__ Private method name >(self, < Parameter list >):
        ...

When defining private methods, you need two underscores before the property name ( __ ).

All kinds of methods can be changed into private methods by adding double down-change lines

Private methods formally protect the Python Function logic used inside the class

Private attributes and disclosure are programmer logic, not security logic, and attach importance to conventions

4. Reserved properties of classes

Python Class properties reserved by the interpreter begin or end with double down lines.

Reserved attributes are also called special attributes Begins and ends with double underscores The function is to understand Python Class provides the attribute interface of unification 1 Attribute values have special meanings, which are directly used after class definition

Use only <类名> Preserved properties accessed

保留属性 描述
__name__ 类的名词
__qualname__ 以.分隔从模板全局命名空间开始的类名称
__bases__ 类所继承的基类名称

5. Retention method of class

The retention method is Python Interpreter reserved method, beginning and ending with double underscores

Reserved methods are also called special methods Begins and ends with double underscores The function is to understand Python Class provides the method interface of unification 1 Method logic: Has a specific meaning, 1 is generally associated with operators, and the class definition needs to be overloaded

Related articles: