In this article, let's look at the modules in the Python programming language. A friend who has just come into contact with this language may not understand
modules in PythonWhat is the meaning of this concept. Then this article will bring you to meet the module. The first thing we need to know
What is a python moduleAnd then we need to know
python module usesIn what way, only by knowing these two concepts can we begin to understand and use modules.
Python module:
The Python module, which is a python file that ends with a. Py, contains Python object definitions and Python statements.
Modules allow you to logically organize your Python code snippets.
Assigning the relevant code to a module will make your code better and easier to understand.
Modules can define functions, classes, and variables, and modules can also contain executable code.
The following example is a simple module support.py:
def print_func (PAR): print "Hello:", par return
Next, let's take a look at the introduction of modules
Once the module is well defined, we can use the import statement to introduce the module with the following syntax:
Import module1[, module2[,... Modulen]
For example, to reference the module math, you can use import math to introduce it at the very beginning of the file. When you call a function in the math module, you must refer to this:
Module name. function name
When the interpreter encounters an import statement, the module is imported if it is in the current search path.
A search path is a list of all directories that an interpreter will search first. To import the module support.py, you need to place the command at the top of the script:
#!/usr/bin/python#-*-coding:utf-8-*-# Importing module Import Support # can now call the function contained in the module Support.print_func ("Runoob")
The result of the above example output:
Hello:runoob
(a module will only be imported once, no matter how many times you have implemented the import.) This prevents the import module from being executed over and over again. )