Advantages and usage summary of python function stored in module

  • 2021-12-05 06:37:00
  • OfStack

1. By storing functions in separate files, you can hide the details of the program code and focus on the high-level logic of the program.

2. It allows you to reuse functions in many different programs.

After you store functions in separate files, you can share those files with other programmers instead of the entire program. Knowing how to import functions also allows you to use function libraries written by other programmers.

Instances

Import all functions in the module


from tags import *
make_tags(' Famous works of world art 210 Speak  :  Illustrated collection ', ' Art ')
print_tags([' Art history ', ' Art '])

When Python reads the file, the code import tags causes Python to open the file tags. py in the background and copy all of its functions into the program.

Note: The file path pointed to by import is the root directory where the project is located. So if *. py is in a subdirectory, you must add the subdirectory path to the file path.

Expansion of knowledge points:

Import a function in the module

We can also import specific functions in the module with the following syntax:


from module_name import function_name

If you need to import any number of functions from a module, you can separate the function names with commas:


from module_name import function_name_0, function_name_1, function_name_2

from tags import make_tags
make_tags(' Famous works of world art 210 Speak  :  Illustrated collection ', ' Art ')

Run results:

book_name= Lecture 210 of World Fine Arts Masterpieces: Collection of Illustrations
tags= ('Art',)


Related articles: