The Python module

Source: Internet
Author: User
Tags list of attributes

A module is a file that contains all of the functions and variables that you define, followed by a. PY name. Modules can be introduced by other programs to use functions such as functions in the module. This is also the way to use the Python standard library.

Modules allow you to logically organize your Python code snippets, and assigning related code to a module can make your code better and easier to understand.

Modules can define functions, classes, and variables, and modules can also contain executable code.

 More model_1.py def p_fun ():   print ('helloworld')   more from  model_1 import  p_funif"__main__":      #     p_fun () run: [[email protected] model]$ python3 main.py Hello World

From ... import *: This will import all the contents of the module into the current namespace, which is generally not recommended, consumes memory space, and is prone to unexpected problems.

1. Search Path

When a module is imported, the Python parser's search order for the module location is:

The current directory----each directory under Shell variable pythonpath----------The Python module path directory

The search path for the module is stored in the Sys.path variable of the system module, including the current directory, Pythonpath, and the default directory that is determined by the installation process.

>>> Import sys>>> print (sys.path) ["'/home/python/ Python3/lib/python37.zip'/home/python/python3/lib/python3.7'  /home/python/python3/lib/python3.7/lib-dynload'

' /home/python/python3/lib/python3.7/site-packages ']

2. Namespaces and Scopes

A variable is a name (identifier) that has a matching object. A namespace is a dictionary that contains the variable names (keys) and their respective objects (values).

A Python expression can access variables in the local namespace and in the global namespace. If a local variable and a global variable have the same name, the local variable overrides the global variable.

Each function has its own namespace. The scope rules of a method of a class are the same as the usual functions.

Python intelligently guesses whether a variable is local or global, and assumes that any variable that is assigned within the function is local.

Therefore, if you want to assign a value to a global variable within a function, you must use the global statement.

The expression for global VarName tells Python that VarName is a global variable so that Python does not look for the variable in the local namespace.

For example, we define a variable money in the global namespace. We then assign a value to the variable money within the function, and then Python assumes that money is a local variable. However, we did not declare a local variable money before the visit, and the result is a unboundlocalerror error. Canceling the comment on the global statement will solve this problem.

 -     #定义全局变量def Addmoney ():    global money       #定义全局变量     = money +1   2000  2001

3.dir () function

The built-in Dir () function lists an identifier for a defined object. For example, for a module, include functions, classes, and variables defined in the module.

When you provide a module name to Dir (), it returns a list of the names defined in that module. When no arguments are provided for it, it returns a list of the names defined in the current module.

4.dir () Function Example:

>>>Import SYS # gets the list of attributes, here is the list of properties for the SYS module>>>Import SYS>>>dir(SYS) ['__breakpointhook__','__displayhook__','__doc__','__excepthook__','__interactivehook__','__loader__','__name__','__package__',
'__spec__','__stderr__','__stdin__','__stdout__','_clear_type_cache','_current_frames','_debugmallocstats','_framework','_getframe'>>>dir() #获得当前模块的属性列表 ['__annotations__','__builtins__','__doc__','__loader__','__name__','__package__','__spec__','SYS']>>> a=2>>>dir()['__annotations__','__builtins__','__doc__','__loader__','__name__','__package__','__spec__',' A ','SYS']>>> del a # Delete/Remove a name>>>dir()['__annotations__','__builtins__','__doc__','__loader__','__name__','__package__','__spec__','SYS']

5.globals () and locals () functions

Depending on where they are called, the Globals () and locals () functions can be used to return names in the global and local namespaces.

If locals () is called inside the function, all the names that can be accessed in the function are returned.

If Globals () is called inside the function, all the global names that can be accessed in the function are returned.

The return type of two functions is a dictionary. So names can be extracted with the keys () function.

6.reload () function

When a module is imported into a script, the code at the top-level part of the module is executed only once.

Therefore, if you want to re-execute the code in the top-level part of the module, you can use the reload () function. The function will re-import the previously imported modules.

Packages in 7.python

A package is a hierarchical file directory structure that defines a Python application environment consisting of a module and a sub-package, and a sub-package under a sub-package.

In a nutshell, a package is a folder, but a __init__.py file must exist under the folder, and the contents of the file can be empty. __int__.py is used to identify the current folder as a package.

[Email protected] model]$ Moreaa.py def munit (x, y): print ('sum:', x+y) return x+Y[[email protected] model]$ Morebb.py def mule (x, y): return x**Y[[email protected] model]$ Moreinit.py Import syssys.path.append ("/home/python/model") from AA import munitfrom bb import muleprint (sys.path) H= Munit (3,4) s= Mule (3,4) print (s) results: [[email protected] model]$ Python3 init.py ['/home/python/model','/home/python/python3/lib/python37.zip','/home/python/python3/lib/python3.7','/home/python/python3/lib/python3.7/lib-dynload
'/home/python/python3/lib/python3.7/site-packages','/home/python/model']sum:7Bayi

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.