Modules in Python

Source: Internet
Author: User

     

C language Programming experience of friends know that in C language if you want to reference sqrt this function, you must use the phrase "#include <math.h>" to introduce math.h this header file, otherwise it will not be called normally. So in Python, what if you want to refer to some of the built-in functions? There is a concept in Python called module, which is similar to a header file in C and a package in Java, such as calling the SQRT function in Python, which must be introduced into the math module with the Import keyword. Here's a look at the modules in Python.

I. INTRODUCTION of Modules

Using the keyword import in Python to introduce a module, such as referencing module math, can be introduced with import math 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

Why do I have to add a module name to call it? Because there might be a situation where a function with the same name is in multiple modules, and if it is only called by the function name, the interpreter cannot know exactly which function to invoke. So if the module is introduced as described above, the calling function must add the module name.

Import math# This will be an error. Print sqrt (2) #这样才能正确输出结果print math.sqrt (2)

Sometimes we just need to use a function in the module, we just need to introduce the function, at this point we can pass the statement

From Module name Import function name 1, function name 2 ....

To implement, of course, you can introduce some constants by not only introducing functions. When introduced in this way, only the function name can be given when the function is called, and the module name cannot be given, but when the two modules contain the same name function, the subsequent introduction overwrites the previous one. In other words, if the function function () is in module A, there is also functional function () in module B, and if you introduce the functions in the first and B functions in a, then when you call function, is to execute the function functions in module B.

If you want to introduce everything in math at once, it can also be done by using the From math import *, but not recommended.

Two. Define your own module

In Python, each Python file can be used as a module whose name is the name of the file.

For example, there is a file test.py that defines the function add in test.py:

#test. Pydef Add (b):    return a+b

In other files, you can import test first, then call it through Test.add (A, B), and of course, by using the From test import Add.

Three. What happens when the module is introduced

First look at an example of the code in the file test.py:

#test. Pydef display ():    print ' Hello World '    display ()    

Introduce the module test in test1.py:

#test1. Pyimport Test

Then run test1.py, which will output "Hello World". That is, when importing a module with import, the code in the introduced module file is executed once. Note, however, that the code in the module file is executed only the first time, because it is loaded only when it is first introduced, which is easy to understand and saves time and memory.

Modules in Python

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.