Python _x __all__ Simulation Imports ' private ' variables and their points of attention and examples

Source: Internet
Author: User
Tags naming convention

A variable that begins with an underscore (such as _x):

Under certain circumstances, it can be underlined that the signature of the variable name (such as _x) prevents the client from copying the name of the module when importing it using the FROM * statement, but the underscore is not a ' private ' declaration: You can still see and modify such variables using other import forms, such as using import

__all__ (statement)

It is also possible to assign a string list of variable names to the variable __all__ at the top of the module to achieve a hidden effect similar to the _x naming convention; Using this feature, the from* statement will only copy these variable names that are listed in the __all__ list


__all__ is the name of the variable that is to be copied, and the variable name that is not copied when _x. Python will first look for a list of __all__ in the module (if you define __ALL__, only the variable names in __all__ will be copied, not the variable names with no underscore at the beginning), and if there is no definition, the from * will copy all variable names with no single underscore at the beginning

The __all__ list is valid only in the form of the FROM * statement, not a private statement

Example:

#test. py

Key= ' Got '

_nooutpute= ' lllllllllllllll '

Error= ' FASDFSA '

Code= ' FSADFSA '

Decode= ' 324324 '

__all__=[' ERROR ', ' Code ', ' decode ']

Print {' already ': (lambda:2+2),

' Got ':(lambda:2*4),

' One ': (lambda:2**4)

}[key] ()

#_X__all__. py

From Test import *

Print (Error)

Print (key)


Note the point:

You can see that the __all__ must be a variable name and must be assigned before referencing it.


#test. PY Change Test __all__

Error= ' FASDFSA '

Code= ' FSADFSA '

Decode= ' 324324 '

__all__=[' ERROR ', ' Code ', ' Decode ', ' testname ']

#_x__all__. py

From Test import *


Print (Error)

Print (testname)




Python _x __all__ Simulation Imports ' private ' variables and their points of attention and examples

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.