Often, you collect functions in a script. This creates a module with additional Python functionality. To demonstrate this, we create a module by collecting functions in a single file, for example, smartfunctions.py:
def f(x):
return 2*x + 1
def g(x):
return x**2 + 4*x - 5
def h(x):
return 1/f(x)
- These functions can now be used by any external script or directly in the IPython environment.
- Functions within the module can depend on each other._
- Grouping functions with a common theme or purpose gives modules that can be shared and used by others.
Again, the command exec(open('smartfunctions.py').read()) makes these functions available to your IPython shell (note that there is also the IPython magic function, run). In Python terminology, you say that they are put into the actual namespace.