Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

The concept of python object-oriented programming Encapsulation

Shulou Source: shulou.com Published: 2022-06-02 06:24:54 09月30日 Update

This article mainly explains the concept of python object-oriented programming encapsulation. The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn the concept of python object-oriented programming encapsulation.

Definition of encapsulation

Encapsulation is an important concept in object-oriented programming. It refers to hiding and protecting some properties and methods in the class so that they can only be used within the class and cannot be directly accessed by the outside.

There are two most important advantages of encapsulation: one is to improve data security, and the other is to improve the robustness of code.

Encapsulation method

Putting a double underscore before a variable defined inside the class means that the variable is private and cannot be accessed directly from the outside.

Class User:__private = 'secret'def _ _ print (): print (f' can't tell you {User.__private}') the principle of encapsulation

Why can't a doubly underlined variable be called from outside the class? Take a closer look at the two variables private and print:

Class User:__private = 'secret'def _ print (): print (f' can't tell you {User.__private}') print (User.__dict__) out: {'_ module__':'_ main__','_ User__private': 'secret',' _ User__print':,'_ dict__':,'_ weakref__':,'_ doc__': None}

Variables that define the beginning of a double underscore within a class are automatically converted by python to a single underscore class name, a double underscore variable name. Private variables cannot be defined outside the class, that is, variables that define the beginning of a double underscore outside the class are not automatically converted. So direct access to private variables outside the class will report an error. Private variables can be accessed directly within the class.

In addition, after knowing the principle of python encapsulation, private variables can actually be accessed directly outside the class. The example is as follows:

Class User:__private = 'secret'def _ print (): print (f' can't tell you {User.__private}') print (User._User__private) User._User__print () out:secret can't tell you secret

However, it is strongly recommended not to access private properties inside the class directly outside the class, which breaks the rules of encapsulation and is not conducive to data security and code robustness.

Encapsulated case

There are a lot of people who use encapsulation. Here is a small example:

Class User:def _ init__ (self, name, password): self.name = name self.__password = passworddef chgpwd (self): pwd = input ('Please enter the old password:') if pwd = = self.__password:pwd1 = input ('Please enter the new password:') pwd2 = input ('Please enter again:') if pwd1 = = pwd2 and len (pwd1) > = 6:self.__password = pwd1return Falsereturn Truen1 = User ('Zhang San') '123456') while n1.chgpwd (): print (' password modification failed Please try again!)

In the above code, _ _ password is a private attribute and cannot be modified directly! To modify it, you must use the chgwd () method, which has the function of verifying the old password and repeating the new password to ensure that the modified password is compliant. Packaging is an indispensable means in a multi-person project. It is often used to verify whether the input data value is reasonable, and so on.

Thank you for reading, the above is the content of "the concept of python object-oriented programming encapsulation". After the study of this article, I believe you have a deeper understanding of the concept of python object-oriented programming encapsulation, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Encapsulation variables private underscores objects concepts programming passwords methods input code data cases learning validation robustness importance security content principles Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft Linux Docker Apple Redmi