Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

Hello everyone, I am a bowl week, a front end that does not want to be drunk (inrolled). If I am lucky enough to write an article that you like, I am very lucky

Encapsulated understanding

Encapsulation: Abstraction of properties and methods

  • Attribute abstraction: Attributes (variables) of a class are defined, isolated, and protected

    There are private attributes and public attributes

    • Private attributes: Can only be accessed within a class
    • Public properties: Accessible by class, object name

    You can choose to expose or hide a property, to hide the underlying mechanism of the property

  • Method abstraction: The definition, isolation, and protection of a class’s methods (functions)

    There are private methods and public methods

    • Private methods: accessible only within the class
    • Public methods: can be accessed by class, object name

    You can choose to expose or hide a method, hiding its internal logic

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

Encapsulation is the process of making data and code called classes, expressed as: class-property-method

Private class attributes, public class attributes, private instance attributes, and public instance attributes

Exposing class attributes

Exposing class attributes is class attributes, syntax structures

Def __init__(self,[parameter 1],[parameter 2],... [parameter n]): self.< instance attribute name > = < instance attribute initial value >...Copy the code

Private class attributes

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

Class class name: <__ private class attribute name > = < private class attribute initial value > def __init__(self,[1],[2],... [parameter n]): self.< instance attribute name > = < instance attribute initial value >...Copy the code

.< class property > or < object name >.< class property >

This effectively ensures the controllability of attribute maintenance

Example code is as follows:

class TestClass: __number = 0 def __init__(self, num_value): for i in range(num_value + 1): TestClass.__number += i@classmethod # 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'Copy the code

__number raises an AttributeError exception when the class is accessed

Expose instance properties

Exposing instance properties is equivalent to example properties, syntactic structure

Class < class name > : < class attribute name > = > < class method value def __init__ (self, parameter list > <) : self. < instance attribute name > = > < instance attribute value...Copy the code

Private instance properties

Private instance attributes can only be used within 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 attribute name > = > < instance attribute value...Copy the code

.< class property > or < object name >.< class property >

This effectively ensures the controllability of attribute maintenance

The 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'
Copy the code

Private properties are not necessarily private

The double underscore of a private attribute is just a conversion convention. After the conversion, the original name of the class changes, which is a form of private

The 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 # This can be accessed via the object name._ class name.__ propertyCopy the code

This can be accessed via the object name._ class name.__ property.

Private and public methods

Defining methods are methods defined and used inside a class. Grammatical structure

Class < class name >: def <__ private method name >(self, < argument list >):...Copy the code

Private method definitions require two underscores (__) before the property name.

Each method can be made private by adding a double drop line

Private methods formally protect the function logic used inside Python classes

Private properties and disclosure are programmer logic, not security logic, and convention is important

Class reserved attributes

A class attribute reserved by the Python interpreter that begins or ends with a double drop line.

  • Reserved attributes are also called special attributes
  • Start and end with a double underscore
  • Provides a unified attribute interface for understanding Python classes
  • Attribute values have special meanings that are used directly after the class definition

Reserved attributes accessed only with < class name >

Retention properties describe
__name__ Class of nouns
__qualname__ In order to.Delimit the class names starting from the template global namespace
__bases__ The name of the base class from which the class inherits

Class retention methods

Reserved methods are reserved by the Python interpreter and begin and end with a double underscore

  • Reserved methods are also called special methods
  • Start and end with a double underscore
  • Provides a unified method interface for understanding Python classes
  • Method logic: has a specific meaning, usually associated with operators, and class definitions need to be overloaded