Learn Python's proprietary functions and methods from the old

  • 2020-04-02 14:17:31
  • OfStack

In any language, it is specified that certain objects (properties, methods, functions, classes, etc.) can only be accessed within a certain scope, beyond which they cannot be accessed. This is the division of "public" and "private". In addition, special representations are specified for special things, such as class names that cannot be used with class, def, etc., which are reserved words. In addition to reserved words, there is something special in python for the name of a class, which is a "proprietary" category.

Private functions

At some point, you'll see that there's a special way to name a function/method that starts with a "! "double underscore, and that this type of named function/method is called a" private function ".

The so-called private function is:

Private functions cannot be called from outside their module
Private class methods cannot be called from outside their classes
Private properties cannot be accessed from outside their classes
The opposite of private is what's called public. Some programming languages use special keywords to indicate whether a function or method or class is private or public. But python USES only names, because python has a deep understanding of what Mr Koon meant when he said "misnamed" 2k years ago.

If the name of a Python function, class method, or property begins (but does not end) with two underscores, it is private. Everything else is public. Class methods are either private (used only in their own classes) or public (used anywhere). Such as:


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

The method defined here is a private method.

Let's refine the above class and run it, calling the private method through an instance


#!/usr/bin/env python
#coding:utf-8 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) # The results Tom
Traceback (most recent call last):
  File "225.py", line 14, in <module>
    officer.__work(1000)
AttributeError: Person instance has no attribute '__work'

As can be seen from the results of the run, an error has been reported when running to office.arbitration work(1000). And from the error message, there is no such method. This means that this private method cannot be called accidentally in a class (although class accidents can call private methods, which is too cumbersome and not recommended, so this tutorial filters out).

The above code is modified as follows:


#!/usr/bin/env python
#coding:utf-8 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)        # Private methods are called inside the class if __name__=="__main__":
    officer = Person("Tom")
    #officer.__work(1000)
    officer.worker() # The results Tom
Tom salary is: 500

The result is exactly what you want. Do you understand the use of private methods?

Proprietary methods

If you start with a double underscore but don't end with it, the named method is private.

If you start with a double underscore and end with a double underscore, the named method is a proprietary method.

That's what python says. So when you write a program, you execute it. If you don't execute it, you'll mess with python. If you don't, you'll report an error.

S), for example, is a typical proprietary method. Then when you write any other method, don't use the beginning and the end. Although used probably no impact, but in the readability of a lot of poor, a paragraph of procedures if the readability is not good, not for a long time to read their own, let alone others?

With respect to the proprietary methods, in addition to the ones with arbitration (), there are those with arbitration (s) such as arbitration (s), arbitration (s), etc. To look, you can use the dir() function to look at the contents of a function in interactive mode. Of course, you can define it yourself.

Since it is used more often, it is used in many of the previous examples.


Related articles: