Python Study Notes (6): Module

Source: Internet
Author: User

We already know that functions can reuse code, so modules can be reused in other programs. A module is basically a file that contains all your defined functions and variables. The file name of the python module must use the. py extension, and the import module uses the Import Statement.

1. Use the SYS module
import sysprint("The command line arguments are:")for i in sys.argv:    print(i)print("\n\nThe PYTHONPATH is", sys.path, "\n")
2. byte-compiled. PyC File

To make the input module faster, Python compiles the. py file into a byte file. PyC. You only need to use the import statement, followed by the file name, that is, the module name, the program will automatically generate a file with the same name. pyC file. The next time you import this module from another program ,. the PyC file takes effect, which is much faster and the files compiled by these bytes are not related to the platform.

3. From... Import Statement

If you want to directly input the argv variable to your program (avoid using SYS. Every time you use it), you can use the from sys import argv statement. If you want to enter the names used by all sys modules, you can use the from sys import * Statement. This applies to all modules. Generally, you should avoid using the import statement instead of from .. import, because this will make your program easier to read and avoid name conflicts.

4. _ name _ of the module __

Each module has a name. In the module, you can use statements to find the module name. This is particularly useful in one scenario-as mentioned above, when a module is input for the first time, the main module of this module will be run. What should we do if we only want to run the main block when the program itself is used, but do not run the main block when it is input by other modules? This can be done through the _ name _ attribute of the module.

if __name__ == "__main__":    print("This program is being run by itself")else:    print("I am being imported from another module")
5. Custom modules
# Filename: mymodule.pydef sayHi():    print("Hi, this is mymodule speaking.")version = "0.1"

Call a custom Module

import mymodulemymodule.sayHi()print(mymodule.version)

Output result:

Hi, this is mymodule speaking.
0.1

6. dir () function

You can use the built-in dir function to list module-defined identifiers. Identifiers include functions, classes, and variables.
When you provide a module name for Dir (), it returns the name list defined by the module. If no parameter is provided, it returns the name list defined in the current module.

>>> Import sys
>>> Dir (sys)
['_ Displayhook _', '_ Doc _', '_ thook _', '_ name __', '_ package _', '_ stderr _', '_ stdin _', '_ stdout _', '_ clear_type_cache ', '_ current_frames', '_ getframe', 'api _ version', 'argv', 'builtin _ lele_names ', 'byteorder', 'Call _ tracing', 'callstats ', 'copyright', 'displaycall', 'dlhandle', 'dont _ write_bytecode', 'exc _ info', 'mongothook', 'exec _ prefix', 'executable ', 'Exit ', 'flags', 'float _ info', 'float _ repr_style ', 'getcheckinterval', 'getdefaultencoding ', 'getfilesystemencoding', 'getprofile', 'getcursionlimit ', 'getrefcount', 'getsizeof ', 'gettrack', 'getwindowsversion', 'hversion', 'int _ info', 'intern', 'maxsize', 'maxunicode ', 'meta _ path', 'modules', 'path', 'path _ hooks', 'path _ importer_cache ', 'platform', 'prefix', 'setcheckinterval ', 'setfilesystemencoding', 'setprofile ', 'setrecorsionlimit', 'settrack', 'stderr', 'stdin', 'stdout', 'subversion', 'version ', 'version _ info', 'waroptions', 'winver ']
>>>

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.