Python common modules

Source: Internet
Author: User

What is a module

In the development of computer programs, as the program code more and more, in a file code will be more and more long, more and more difficult to maintain.

In order to write maintainable code, we grouped many functions into separate files so that each file contained relatively few code, and many programming languages used this way of organizing code. in Python, a. py file is called a module.

What are the benefits of using modules?

The greatest benefit is that the maintainability of the code is greatly improved. Second, writing code does not have to start from scratch. When a module is written, it can be referenced elsewhere. When we write programs, we often refer to other modules, including Python built-in modules and modules from third parties.

The use of modules also avoids conflicting function names and variable names. Functions and variables of the same name can exist in different modules individually, so we do not have to think about names that conflict with other modules when we write our own modules. But also be aware that try not to conflict with the built-in function name. Click here to see all of Python's built-in functions.

You may also think, what if different people write the same module name? To avoid module name collisions, Python introduces a way to organize modules by directory, called packages.

For example, a abc.py file is a named abc module, and a xyz.py file is a named xyz module.

Now, assuming that our abc and xyz these two module names conflict with the other modules, we can organize the modules through the package to avoid conflicts. The method is to select a top-level package name, for example mycompany , to be stored in the following directory:

Once the package is introduced, all modules will not conflict with others as long as the package name in the top layer does not conflict with others. Now, the name of the abc.py module becomes mycompany.abc , similarly, xyz.py the module name becomes mycompany.xyz .

Please note that each package directory will have a __init__.py file that must exist, otherwise Python will use this directory as a normal directory, not a package. __init__.pyit can be an empty file, or it can have Python code, because __init__.py it is a module, and its module name is mycompany .

Similarly, there can be multi-level catalogs, which form a multi-level package structure.

When you create a module, you should be careful about naming it and not conflict with the module name that comes with Python. For example, the system comes with a sys module, its own module can not be named sys.py , or you will not be able to import the system's own sys module.

Module Import

The import module actually tells the Python interpreter to explain the py file.

    • Import a py file, the interpreter interprets the py file
    • Import a package, the interpreter interprets the __init__.py file under the package
1 Import Module 2  from Import xx 3  from Import xx as rename   4  from Import *

1 Import Statement

import module1[, module2[,... Modulen]

Doing so does not directly import the function in the module into the current semantic table; it simply introduces the module name.

In the current directory to create a file called fibo.py, enter the following content:

1 #Fibonacci Numbers Module2 3 defFIB (n):#Write Fibonacci series up to n4A, b = 0, 15      whileb <N:6         Print(b, end=' ')7A, B = B, A +b8     Print()9 Ten defFIB2 (n):#return Fibonacci series up to n Oneresult = [] AA, b = 0, 1 -      whileb <N: - Result.append (b) theA, B = B, A +b -     returnResult

This function can be accessed by the module name as follows:

1 Import Fibo 2 >>> fibo.fib (+)3 1 1 2 3 5 8 144 233 377 610 9874 >> ;> fibo.fib2 (5) [1, 1, 2, 3, 5, 8,, A,, $]6 >>> fibo.
   
    __name__
    7
    '
    fibo
    '
   

If you intend to use a function frequently, you can assign it to a local variable:

1 >>> fib = fibo.fib2 >>> fib (3) 1 1 2 3 5 8 13 21 34 55 89 144 233 377

2 from.. Import statement

 from import name1[, name2[, ... Namen]]

This declaration does not import the entire ModuleName module into the current namespace, but only introduces the name1 or name2 individual inside it into the global symbol table of the module that executes the declaration.

1  from Import fib, Fib2 2 >>> fib (3) 1 1 2 3 5 8 13 21 34 55 89 144 233 377

Python common modules

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.