Let's talk about Python private properties and private methods

WBOY
Release: 2023-04-12 11:37:08
forward
1370 people have browsed it

Let's talk about Python private properties and private methods

1. Scenario definition

Private attributes

means that in the object-oriented development process of Python, some attributes of the object are only available in the object are used internally, but do not want these properties to be accessed externally.

That is: private attributes are attributes that the object is not willing to make public.

Private methods

means that in the object-oriented development process of Python, some methods or functions of the object only want to be used inside the object, but do not want to be accessed externally. these methods or functions.

That is: a private method is a method or function that the object does not want to make public.

2. Syntax definition

The syntax for defining private properties and private methods in Python is as follows:

class Staff:
def __init__(self, s_name, s_salary):
self.s_name = s_name
self.__salary = s_salary

def __secret(self):
print("%s 的工资是 %d" % (self.s_name, self.__salary))
Copy after login

(1). __salary is defined starting with two underscores Private property.

(2). __secret(self) is a private method defined starting with two underscores.

3. Call analysis

(1). In the object initialization method of __init__, the __salary attribute defined starting with two underscores is a private attribute.

Now call the __salary attribute outside the object to see if the private attribute can be accessed normally.

Let's talk about Python private properties and private methods

As can be seen from the running results in the above figure, line 11, that is, when accessing the private attribute __salary of the object outside the object, an AttributeError error is prompted, and the Staff object zhangsan has no attributes. __salary.

In order to prove that the Staff class object does have the instance attribute __salary, it is just because the private attributes cannot be accessed outside the object.

I modified self.__salary to: self.salary, the __secret(self) method references the self.__salary attribute, and made corresponding modifications. See the running results as shown in the figure below.

Let's talk about Python private properties and private methods

It can be seen from the running results that the external call of this non-private attribute is normal and no AttributeError error is prompted.

(2). In the __secret(self) instance method, the __secret(self) method defined starting with two underscores is a private method.

Same as the above test process, first call the private method __secret(self) outside the object to see if the private method can be called normally.

Let's talk about Python private properties and private methods

As can be seen from the running results in the above figure, line 11, that is, when accessing the private method __secret(self) of the object outside the object, an AttributeError error is prompted, the Staff object zhangsan does not have a __secret method.

To prove that the Staff class object has the instance method __secret(self), just because the private method cannot be accessed outside the object.

I modified the __secret(self) method to: secret(self), and other codes remain unchanged. See the running results as shown in the figure below.

Let's talk about Python private properties and private methods

It can be seen from the running results that the external call of this non-private method is normal and no AttributeError error is prompted.

(3). As can be seen from the figure below, private methods and private properties can be called inside the object.

The work method in the figure calls the private method __secret(self), and the private method __secret(self) calls the private attribute __salary.

Let's talk about Python private properties and private methods

Use the Staff class object zhangsan outside the object to call the work method, which can indirectly access the private properties and private methods of the object.

From the console output, it can be seen that the work method can normally access the private properties and private methods defined inside the object.

4. Python pseudo-private properties and private methods

In Python, there is no real sense of privateness, because Python internally makes some special names when naming properties and methods. Processing makes the corresponding properties and methods inaccessible to the outside world.

Taking private attributes and private methods as an example, Python’s internal processing method is:

(1). Attribute: __salary, the processed attribute name is: _Staff__salary(_class name__ Attribute name)

(2). Method: __secret, the processed method name is: _Staff__secret(_class name__method name)

I know Python internally for private attributes and private Method processing, now use this processed naming method to access private properties and private methods outside the object to see if the access is normal.

class Staff:
def __init__(self, s_name, s_salary):
self.s_name = s_name
self.__salary = s_salary

def __secret(self):
return "%s的工资是 %d" % (self.s_name, self.__salary)
zhangsan = Staff("张三", 10000)
print(zhangsan._Staff__salary)
print(zhangsan._Staff__secret())
Copy after login

The running results are shown in the figure below

Let's talk about Python private properties and private methods

The console did not throw any exceptions, and the previous AttributeError error message disappeared.

This example proves that Python is not private in the true sense. After knowing its internal processing method, you can still use the _class name__ attribute name (method name) method to access outside the object. To the private properties and private methods defined inside the object.

But this method is not recommended in daily work. Since when properties and methods are defined inside the object, they are declared private, and the caller needs to abide by its rules.

I just want to use this small example to illustrate that Python does not have real privacy.

The above is the detailed content of Let's talk about Python private properties and private methods. For more information, please follow other related articles on the PHP Chinese website!

source:51cto.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!