Learn Python private functions and proprietary methods

Source: Internet
Author: User
This article is an article in the Python series by Qi. it mainly introduces private functions and proprietary methods. if you need it, you can refer to it in any language, certain objects (attributes, methods, functions, classes, etc.) can only be accessed within a certain range. This is a distinction between "public" and "private. In addition, some special representations will be specified for some special things. for example, the class name cannot use class, def, etc. this is the reserved word. In addition to reserved words, python also makes some special preparations for class names, that is, the category of "proprietary.

Private functions

In some cases, we can see that there is a special method named, which starts with a "_" double dashes and calls such named functions/methods "private functions ".

The so-called private function is:

Private functions cannot be called outside their modules.
Private class methods cannot be called from outside their classes
Private attributes cannot be accessed from outside their classes
Private is the so-called public. Some programming languages use special keywords to indicate whether a function or method or class is private or public. However, python only uses its name, because python has a profound understanding of what Mr. Kong said two K years ago: "The name is not proper.

If a Python function, class method, or attribute name starts with two underscores (but not ends), it is private; all others are public. Class methods are either private (only available in their own classes) or public (available anywhere ). For example:

The code is as follows:


Class Person:
Def _ init _ (self, name ):
Self. name = name

Def _ work (self, salary ):
Print "% s salary is: % d" % (self. name, salary)

The _ work () method defined here is a private method.

The following describes how to improve the class and then run it to call this private method through an instance.

The code is 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)

# Running result

Tom
Traceback (most recent call last ):
File "225.py", line 14, in
Officer. _ work (1000)
AttributeError: Person instance has no attribute '_ work'

It can be seen from the running results that an error is reported when the job is run to officer. _ work (1000. This method is not available in the error message. This shows that this private method cannot be called unexpectedly in the class (in fact, it is too troublesome to call a private method in a class accident, and it is not recommended, so this tutorial filters it out ).

The above code is modified as follows:

The code is 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) # Call private methods within the class

If _ name __= = "_ main __":
Officer = Person ("Tom ")
# Officer. _ work (1000)
Officer. worker ()

# Running result

Tom
Tom salary is: 500

The results are exactly expected. Do you understand the usage of private methods?

Proprietary method

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

If the name starts with double dashes and ends with double dashes, the method named is a proprietary method.

This is stipulated by python. Therefore, when writing a program, you must execute it. if you do not execute it, it means you cannot pass python. if you do not run it, an error is returned.

For example, the _ init _ () mentioned above is a typical proprietary method. When writing other methods, do not start or end. Although it does not affect the readability of a program, the readability of a program is much worse. if the readability of a program is poor, it will take a long time for you to understand it. what's more, what about others?

For the proprietary method, besides _ init _ (), there are also :__ str __,__ setitem _. to look at it, you can use dir () in interactive mode, the function looks at the exclusive items in a function. Of course, you can also define it yourself.

Because _ init _ is used in many cases, it is used in many previous examples.

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.