5. python modules and python modules

Source: Internet
Author: User

5. python modules and python modules

If _ name __= = '_ main _': Usage:

When we run the module file in the command line, the Python interpreter puts a special variable__name__Set__main__If you importhelloModule,ifThe Judgment will fail. ThereforeifThe test allows a module to execute some additional code when running through the command line. The most common is to run the test.

if __name__=='__main__':    test()
Scope

In a module, we may define many functions and variables, but some functions and variables we want to use for others, and some functions and variables we want to use only within the module. In Python_Prefix.

Normal Functions and variable names are public and can be directly referenced, for example:abc,x123,PIAnd so on;

Similar__xxx__Such variables are special variables that can be directly referenced, but have special purposes, such__author__,__name__Is a special variable,helloThe module-defined document annotations can also use special variables.__doc__Access, we generally do not use this variable name for our own variables;

Similar_xxxAnd__xxxSuch functions or variables are private and should not be directly referenced, such_abc,__abcAnd so on;

The reason we say that private functions and variables should not be directly referenced, rather than being directly referenced, is because Python does not have a way to completely restrict access to private functions or variables, however, in programming habits, private functions or variables should not be referenced.

Private functions or variables should not be referenced by others. What are their functions? See the example below:

def _private_1(name):    return 'Hello, %s' % namedef _private_2(name):    return 'Hi, %s' % namedef greeting(name):    if len(name) > 3:        return _private_1(name)    else:        return _private_2(name)

We make public in the modulegreeting()Function, and the internal logic is hidden using the private function.greeting()Functions do not need to care about the details of internal private functions. This is also a very useful method for code encapsulation and abstraction, namely:

All functions that do not need to be referenced externally are defined as private. Only functions that need to be referenced externally are defined as public.

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.