This is the 23rd day of my participation in Gwen Challenge

1. An overview of the

Python has no strict access control restrictions on class members, which can be accessed publicly by default.

Then we have some core code is confidential, can not provide to the customer, but also to help the customer to use, how to do?

At this point, encapsulation comes to mind as one of the object-oriented features.

What is encapsulation?

Encapsulation means that the state information of an object is hidden inside the object, and external programs are not allowed to directly access the internal information of the object. Instead, they operate and access the internal information through methods provided by the class.

Encapsulation is essentially a black box approach to the outside world. Provide only usable interfaces to the outside world, and don’t care about the internal implementation.

So, what are the benefits of encapsulation?

  • Hide the implementation details of the class
  • Allow users to access data only through pre-determined methods, so that control logic can be added to that method to limit unreasonable access to attributes
  • Data checks can be performed to help ensure the integrity of object information
  • Easy to modify, improve code maintainability

How do you implement encapsulation?

Python introduces private properties and methods that hide hidden objects from the outside world.

Ok, XDM, we are going to learn about private properties and private methods

2. Introduction to private attributes (methods)

Private properties and private methods:

  1. In general, we agree that attributes that begin with two underscores are private. Others are public.
  2. Private properties and methods can be accessed inside the class
  3. Private properties (methods) cannot be accessed directly from outside the class
  4. Private properties (methods) can be accessed from outside the class by __ class name __ private property (methods) name

Note:

Methods are essentially properties that can be executed through ()

3. Private attributes (methods) underlying mechanism

Python’s default member functions and variables are public, without providing keywords such as public and private

When we define a private variable or method in Python, we need to prefix the name with __.

A name mangling technique is used internally in Python to replace __membername with _classname__membername, so if you use the original private member’s name externally, you will be told it is not found

Here’s an example:

Class Animal: def __init__(self): self.__name = 'Tom' # Print (" name is {0}". Format (self.__name)) def get_age(self): Print ("{0} age is {1}". Format (self.__name,self.age)) cat = Animal() cat.get_age() cat.__get_name() If the instance calls a private method in Python, it will be able to access itCopy the code

  1. The Animal class is created in the heap memory. Constructor instance attributes have public age and private __name; The __get_name private and get_age public methods in instance methods

  1. Create an instance of cat from Animal() and initialize the instance attribute. Since __name is private, python internally replaces this attribute with _Animal__name.

  1. Private methods of the same kind are also renamed internally by Python. The private __get_name method is changed to _Animal__get_name(). Public methods do not change their names

  1. As we execute the code above. Instance objects call normal methods and call private methods or properties, but Python will throw an exception indicating that they were not found

  1. If we want the outside world to access a private method or property, we can use Python to rename the _classname__ private method (private property).

Detailed instructions

__name is a private property and __get_name() is a private method.

If you want to access private data, you can access it. Strictly speaking, private methods are accessible outside of their classes, but they are not easily handled.

Nothing is truly private in Python; Internally, the names of private methods and properties are suddenly changed and restored, making them seem unusable with their given names

conclusion

Python does not provide the same private modifiers as other languages such as C++, so Python does not really support hiding. To better hide class members, private properties, and private methods.

Private attributes and private methods, can better improve the maintainability of the code, easy to modify, and will not affect the public method, we in the daily work, to practice ~

All right, that’s all for this episode. You’re welcome to comment in the comments section. See you next time