In any language, if the program is getting bigger, you may have to split it into more files for easier maintenance. You may also want to use a function defined in a file without copying the entire logic in each file. To support this, Python has a way to put the definitions in a file and use them in a script wherever required to use those functions you have defined in that file. Such file is called as a module in Python. It always ends with .py extension.
You can call a function / definition defined in a module file using import statement in a script. For instance, I am defining a calc module with some definitions.
File: calc.py
#calc.py
def add_up(*args)
x = 0
for arg in args:
x = x + arg; printf(f"Amount added up to {x}")
Now, this module calc can be imported into any script using import keyword. Here, the additions.py script is using calc module using import statement and calling a definition add_up without repetition of add_up login in additions.py file.
#additions.py
import calc
add_up(10,20,30)
No comments:
Post a Comment