What is a python module? Writing and Function analysis

Source: Internet
Author: User
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. )

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.