First, call the parent function and checks the results:
>>> first_funct() 1 2
Next, call the nested function directly and notice that an error is received:
>>> second_funct() Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'second_funct' is not defined
To work with another module, import the desired module:
>>> import math
Below, we call the sin() function from within the module in the form <module>.<function>:
>>> math.sin(45) 0.8509035245341184
Try calling a function, as demonstrated below, without using the dot-nomenclature to specify its library package results in an error:
>>> sin(45)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'sin' is not defined
Alternatively, the example below shows how to import all items from a module using the * wildcard to place the items within the current program's namespace:
>>> from math import *
>>> sin(45)
0.8509035245341184
A common way to run modules as scripts is to simply call the module explicitly from the command line, providing any arguments as necessary. This can be set up by configuring the module to accept command-line arguments, as shown in print_funct.py:
def print_funct(arg):
print(arg)
if __name__ == "__main__":
import sys
print_funct(sys.argv[1])
print_mult_args.py shows that, if more than one argument is expected, and the quantity is known, each one can be specified using its respective index values in the arguments list: