Friday, July 20, 2018

Jython - Modules

Jython - Modules


A module is a Jython script in which one or more related functions, classes or variables are defined. This allows a logical organization of the Jython code. The Program elements defined in a module can be used in another Jython script by importing either the module or the specific element (function/class) from it.
In the following code (hello.py) a function SayHello() is defined.
#definition of function
defSayHello(str):
   print "Hello ", str
   return
To use the SayHello() function from another script, import the hello.py module in it.
import hello
hello.SayHello("TutorialsPoint")
However, this will import all functions defined in the module. In order to import specific function from module use following syntax.
from modname import name1[, name2[,... nameN]
For example, to import only the SayHello() function, change the above script as follows.
from hello import SayHello
SayHello("TutorialsPoint")
There is no need to prefix the name of the module while calling the function.

No comments:

Post a Comment