Python module = = namespace?

Source: Internet
Author: User

Cause: You want to use a module to pass a variable, modify the value of a variable, and be visible in other modules
So I did an experiment like this:
[Email Protected]:vearne/test_scope.git

base.py

value = 10

b.py

import basedef hello():    print‘scope base‘, base.value, id(base.value)

main.py

frombasevaluefrom‘scope base‘value, id(value)value20‘scope local‘value, id(value)hello()

Run Python main.py
The output results are as follows:

[' __builtins__ ',' __doc__ ',' __file__ ',' __name__ ',' __package__ ',' Hello ',' value ']scopeBase Ten 140195531889072[' __builtins__ ',' __doc__ ',' __file__ ',' __name__ ',' __package__ ',' Hello ',' value ']scope Local - 140195531888832ScopeBase Ten 140195531889072

As you can see, the value of value is not modified and the ID value (the object's memory address) is inconsistent, so we conclude that value and base.value exist in different locations and are two different objects.
Read the official Python documentation
Https://docs.python.org/2/tutorial/modules.html
I found something like this.

Each module have its own private symbol table, which are used as the global symbol table by all functions defined in the MoD Ule. Thus, the author of a module can use global variables in the module without worrying on accidental clashes with a user ' s global variables. On the other hand, if you know what is doing you can touch a module ' s global variables with the same notation used to Refer to its functions, modname.itemname.

Modules can import other Modules. It is customary and not required to place all import statements at the beginning of a module (or script, for that matter). The imported module names is placed in the importing module ' s global symbol table.

Each module has its own symbol table, and when we introduce a module, the contents of the symbol table are modified, and Dir () can be used to view the list of symbols in the symbol table of the current module.
Look at the following:

print‘------------------‘15print dir()print‘------------------‘import mathprint dir()print‘------------------‘fromimport timedeltaprint dir()

The results of the operation are as follows:

------------------[' __builtins__ ',' __doc__ ',' __file__ ',' __name__ ',' __package__ ',' A ']------------------[' __builtins__ ',' __doc__ ',' __file__ ',' __name__ ',' __package__ ',' A ',' Math ']------------------[' __builtins__ ',' __doc__ ',' __file__ ',' __name__ ',' __package__ ',' A ',' Math ',' Timedelta ']

Finally, go back to the previous example:

20  # 这里我们并没有修改模块base中value的值,而是重新定义了一个本地变量, 并且符号表中的指向已经被修改了(指向一个本地变量value)...

In fact, the Python module looks a bit like a namespace, isolating variables with the same name, preventing naming conflicts

Resources:
Https://docs.python.org/2/library/datetime.html

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Python module = = namespace?

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.