python class package - Detailed class private variables, and static properties private

When the procedure is completed, some data and code we do not want people to call or the time to see, and a program package, mainly the privatization of variable or method in python.
We can add __ in front of variables or methods, two underscores to be privatized, so that it becomes private variables or private methods, so that the outside can not be called directly, and the privatization of the class content of 3, are
1) privatization variable
2) the method of privatization
3) static variables
1. under the first privatization of variables, such as the following code

class Person:
    def __init__(self,name):
        self.name=name
man=Person('wk')
print(man.name) 

The results are as follows:
wk
example above, we define a simple class, call the name attribute, which can be seen after instantiation. But if we change the name __name, previous methods can not be called. such as:

class Person:
    def __init__(self,name):
        self.__name=name
man=Person('wk')
print(man.name)

Results The following error occurs

By normal method invocation, we did not get a variable name, but not completely available, we can finally calling code to read:

print(man._Person__name)

This is the result of the call right, so, when we privatized the variable after the class, are summarized as follows:
1) method before class variables can not be used outside the class call
2) When the need to call in an external, need __ _ using the class name to call the variable name format
3) class variable privatization is not really can not call outside, but for a new way of expression

2. When the name of our former method in class __ increase, we also can not be called directly from outside the class, and to call the class name in the format _ __ variable name

class Person:
    def __init__(self,name):
        self.name=name
    def __walk(self,speed):
        return speed
man=Person('wk')
s=man._Person__walk(10)
print(s)

When internal call, follow the same principles previously, such as:

class Person:
    def __init__(self,name):
        self.__name=name
    def __walk(self,speed):
        str=speed+self.__name
        return str
man=Person('wk')
s=man._Person__walk('10')
print(s)

We call the method name variable in the walk, it must be through self .__ name expression, otherwise the program will complain

3. private static properties. With the front is the same, for example, we define __key = 'aaa', other than ours can not directly call key to view the parameters, and the interior is in use, should follow the self .__ key way, or will be error, such as :

class Person:
    __key='aaa'
    def __init__(self,name):
        self.__name=name
    def __walk(self,speed):
        str=speed+self.__name+self.__key
        return str
man=Person('wk')
s=man._Person__walk('10')
print(s)

Simple summary:
1. The definition of private class variables, methods or static properties, can be carried out in accordance with the increase in dual-line decline in the way before the name
2. definition, can not be called directly from outside the class, use the class name _ __ variables call format name.
3. The package can be used in class, block certain key parameters

Published 13 original articles · won praise 1 · views 192

Guess you like

Origin blog.csdn.net/aa12551827/article/details/104858317