Private functions and proprietary methods with old Ziko python

Source: Internet
Author: User
In any language, certain objects (properties, methods, functions, classes, and so on) can be accessed only within a certain scope, which is inaccessible. This is the "public", "private" points. In addition, there will be special for certain special things, such as the name of the class can not be used class,def, and so on, this is the reserved word. In addition to reserved words, Python also makes some special preparations for the name of the class, which is the "proprietary" category.

Private functions

At some point, you will see that there is a way to name a particular, "__" The beginning of the double line, the name of such a function/method called "Private function."

The so-called private function is:

Private functions cannot be called from outside their modules
Private class methods cannot be called from outside their classes
Private properties cannot be accessed from outside their classes
The private counterpart is the so-called public. Some programming languages use special keywords to indicate whether a function or method or class is private or public. But Python is only described by name, because Python has a deep understanding of what Mr. Chu So said in 2k years ago.

If a Python function, the class method, or the name of the property starts with two underscores (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 public (can be used everywhere). For example:

Copy the 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 following class is refined, then run, using an instance to invoke this private method

Copy the 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):
File "225.py", line +, in
Officer.__work (1000)
Attributeerror:person instance has no attribute ' __work '

As can be seen from the running results, when running to Officer.__work (1000), the error. And from the error message, said, there is no such method. This means that this private method cannot be called unexpectedly in the class (in fact, the class can invoke the private method unexpectedly, is too troublesome, and does not advocate, so this tutorial filter).

The code above is modified to become:

Copy the 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 what is to be obtained. Does crossing 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 a proprietary method.

This is the Python rule. So in the writing process to execute, do not execute is with Python, can not pass the error.

The __init__ (), as mentioned above, is a typical proprietary method. Then, when you write other methods, do not use __ start and end. Although the use of also probably no impact, but in the readability of a lot of, a program if the readability is not good, not much time to see their own, not to mention others?

With regard to proprietary methods, out of __init__ (), there are, for example, __str__,__setitem__ and so on, to see that the Dir () function can be used in interactive mode to look at the proprietary things in a function. 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.