Scope of the Python module

Source: Internet
Author: User

Scope: variable, function

public (public): can be directly referenced, such as: ABC, PI, test (), similar to __xxx__ such a special variable. Lenovo : In the C language, when something in the module wants to be referenced externally, it is preceded by an extern, or it can be added without extern

private (Secret): should not be directly quoted, such as: _xxx/__xxx variables such as Lenovo: In C, when something in the module does not want to be referenced externally, the static

is added in front of it.


What do I do when there are variables or functions in a module that you do not want to be referenced by others? As follows:

Private.py is the modulated module

public.py is a public module


#private.py

#!/usr/bin/env Python3
#-*-Coding:utf-8-*-

def _private_1 (name):     #该函数为秘密函数

    return ' Hello,%s '% name

def _private_2 (name):    #该函数为秘密函数
    return ' Hi,%s '% name

def greeting (name= "):    #该函数为公开函数, can be called
    if Len (name) > 3:
   & nbsp;    return _private_1 (name)
    else:
         return _private_2 (name)


If __name__== ' __main__ ':
Print (greeting ())






#public. py

#!/usr/bin/env Python3
#-*-Coding:utf-8-*-

From private import greeting #导入private模块的greeting函数

Print (Greeting ([' You ', ' very ', ' nice '))

Scope of the Python module

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.