Python's modules

Source: Internet
Author: User
Tags pow sin

  Python module and package concept : The package contains modules, modules are saved in xxx.py format if a package code is called Import xxx. If you want to specify the module code in the calling package, import the package. Module. Calling a function in a module is a must to indicate the path of the call, package. module. function.

Import Modules

  

To use a module, we must first import the module. Python imports a module using the import statement. For example, import the module that comes with the system, math:

Import Math

You can think of math as a variable that points to the imported module, where we can access all the exposed functions, variables, and classes defined in the Math module:

>>> Math.pow (2, 0.5) # POW is a function 1.4142135623730951>>> Math.PI # PI is a variable 3.141592653589793

If we only want to import some of the functions of the math module used, instead of all the functions, you can use the following statement:

From math import pow, sin, log

In this way, the 3 functions of pow, sin, and log can be referenced directly, but other functions of math are not imported:

>>> Pow (2, ten) 1024.0>>> sin (3.14) 0.0015926529164868282

What if I encounter a name conflict? For example, the Math module has a log function, logging module also has a log function, if used simultaneously, how to resolve the name conflict?

If you import the module name using import, there is no conflict because the function name must be referenced through the module name:

import Math, Loggingprint math.log   # called the log function of math logging.log (' something ')   # The logging log function is called.

If you use From...import to import the log function, it is bound to cause conflicts. At this point, you can give the function an "alias" to avoid the conflict:

The log of fromMath import log  # Logging now becomes Loggerprint log (Ten)   # called the Loglogger of Math, ' import from Logging ')   # is called the log of logging
Dynamic Import Module
  

If the imported module does not exist, the Python interpreter will report a importerror error:

>>> Import Somethingtraceback (most recent):  File ' <stdin> ', line 1, in <module>import Error:no module named something

Sometimes, two different modules provide the same functionality, such as Stringio and Cstringio , which provide Stringio.

This is because Python is a dynamic language that interprets execution, so Python code runs slowly.

If you want to improve the speed of Python code, the simplest way is to rewrite some key functions in C, which can greatly improve the execution speed.

The same functionality, Stringio is written in pure Python code, while the Cstringio part of the function is written in C, so Cstringio runs faster.

With Importerror errors, we often import modules dynamically in Python:

Try: From    Cstringio import stringioexcept importerror: from    stringio import Stringio

The above code attempts to import from Cstringio first, and if it fails (for example, Cstringio is not installed), try importing from Stringio. This way, if the Cstringio module is present, we will get a faster run, and if Cstringio does not exist, the code will run slower, but it will not affect the normal execution of the code.

The action of the try is to catch the error and execute the except statement when the specified error is caught.

using the--future--function
  
  to introduce a 3.x division rule in Python 2.7, import__future__of theDivision:
>>> from __future__ import division>>> print 10/33.3333333333333335

When a feature of a new version is incompatible with an older version, the feature will be added to the old version in the __future__ so that the old code can test the new feature in the old version.

Python's modules

Related Article

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.