Python private functions and proprietary methods

  In any language, will be prescribed some objects (properties, methods, functions, etc.) can access only within a certain range, out of this range it can not be visited. This is the "public" and "private" parts. In addition, there will be specifically designated for certain special something special representation, such as the class name not be used class, def etc., which is a reserved word. In addition to reserved words, python also made some special preparations for the name of the class, it is the "exclusive" category.

Private functions

  At some point, you will see there is a method name rather special, is "__" double-crossed beginning, the naming of this type of function / method called "private functions."

  The so-called private function, is this: private function can not be called from outside their module Private class methods can not be called from outside their class private property can not be accessed from outside their class with private correspondence, the so-called public friends . Some programming language with a special key words to illustrate a function or method or class is private or public. But the python explained only by name, because a deep understanding of the python Mr. Kong Qiu said "correct words ring true" meaning 2k years ago.

  If a python function name, class methods, or properties beginning (but not end) with two underscores, it's private; all others are publicly owned. Class methods are either private (accessible only in their own class) or public (accessible from anywhere).

E.g:

class Person:

    def __init__(self, name):
        self.name = name
        print(self.name)

    def __work(self, salary):
        print("%s salary is: %d" % (self.name, salary))


if __name__ == "__main__":
    officer = Person("Tom")
    officer.__work(1000)

operation result:

Tom
Traceback (most recent call last):
  File "C:/Users/Administrator/PycharmProjects/Myworld/work.py", line 13, in <module>
    officer.__work(1000)
AttributeError: 'Person' object has no attribute '__work'

As it can be seen from the results of the operation, when the operation officer .__ work (1000) when the error. And said that from the error message, not the method. This shows that the private method can not be called out of class (in fact outside the class can call the private method is too much trouble, moreover do not advocate).

The following code above modifications, be:

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

    def __work(self, salary):
        print("%s salary is: %d" % (self.name, salary))

    def worker(self):
        self.__work(500)  # 在类内部调用私有方法


if __name__ == "__main__":
    officer = Person("Tom")  # Tom
    # officer.__work(1000)
    officer.worker()  # Tom salary is: 500

It is the result to be obtained.

Proprietary methods

If you are beginning with double dashes, but not in it at the end, the naming method is a private method;

If the double-dashed line beginning and ending with a double chain line, a method is named proprietary methods.

For example __init __ (), it is a typical proprietary methods. Then write yourself another way, do not use __ the beginning and end. Although the use also probably have little impact, but in a lot worse readability, readability if a program is not good, will not take long and that they can not read, let alone others?

About a proprietary method, the __init __ (), there are such: __ str __, __ setitem__ etc., want to see, you can use dir () function is a function inside look at proprietary stuff in interactive mode.

 

Guess you like

Origin www.cnblogs.com/maplethefox/p/11615297.html