With old Ziko Python's private functions and proprietary methods _python

Source: Internet
Author: User
Tags reserved

In any language, certain objects (properties, methods, functions, classes, and so on) can only be accessed within a certain range and cannot be accessed out of this scope. This is the "public", "private" points. In addition, it will specifically for some special things to specify some special representations, such as the name of the class can not be used class,def, which is reserved words. In addition to reserved words, Python also makes some special preparations for the name of the class, which is the category of "proprietary".

Private functions

At some point, you'll see a way to name something special, starting with the "__" double line, calling this kind of named function/method "private".

The so-called private function is:

Private functions cannot be invoked from outside their modules
Private class methods cannot be invoked from outside their classes
Private properties cannot be accessed from outside their classes
The corresponding to the private, is the so-called public. Some programming languages use special keywords to illustrate whether a function or method or class is private or public. But Python only uses the name to explain, because Python deeply understood the 2k years ago Mr. Kong Chu So said "the name is not straight" meaning.

If a Python function, the class method, or the name of the attribute starts with a two underscore (but not the end), it is private; all else is public. Class methods are either private (they can only be used in their own classes) or are public (available anywhere). For example:

Copy Code code as follows:

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 __work () is a private method.

The above class is refined and then run to invoke this private method through an instance

Copy Code code 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)

If __name__== "__main__":
Officer = person ("Tom")
Officer.__work (1000)

#运行结果

Tom
Traceback (most recent call last):
File "225.py", line, in <module>
Officer.__work (1000)
Attributeerror:person instance has no attribute ' __work '

From the results of the operation can be seen, when running to Officer.__work (1000), the error. And from the error message that the method is not. This means that this private method cannot be called unexpectedly in the class (in fact, class accidents can invoke private methods, which is too cumbersome, and not advocated, so this tutorial filter).

The following code is modified to be:

Copy Code code 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 (+) #在类内部调用私有方法

If __name__== "__main__":
Officer = person ("Tom")
#officer. __work (1000)
Officer.worker ()

#运行结果

Tom
Tom Salary is:500

The result is to be obtained. Does reader understand the use of private methods?

Proprietary methods

If it starts with a double dash, but does not end with it, the named method is a private method;

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

This is the Python rule. So when you write the program to execute, do not execute is to do with Python, can not pass the error.

For example, the __init__ () mentioned above repeatedly is a typical proprietary method. Then when you write other methods, do not use the beginning and end of __. Although the use of may not have any effect, but in the readability of a lot of, a program if the readability is not in a long time to see themselves do not understand, not to mention others?

With regard to proprietary methods, out of __init__ (), there are other things like: __str__,__setitem__, and so on, to see, you can use the Dir () function to look at the proprietary in a function in interactive mode. Of course, you can define it yourself.

Because __init__ use more, so many of the previous examples are it.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.