Jython 简明教程
Jython - Package
包含一个或多个 Jython 模块的任何文件夹都被识别为一个包。但是,它必须有一个名为 $\{\s0\}$ 的特殊文件,该文件提供要使用的函数索引。
Any folder containing one or more Jython modules is recognized as a package. However, it must have a special file called init.py, which provides the index of functions to be used.
我们现在了解一下如何创建和导入包。
Let us now understand, how to create and import package.
$\{\s1}$ − 创建名为 $\{\s2\}$ 的文件夹,然后创建并保存以下 $\{\s3\}$ 模块:
Step 1 − Create a folder called package1, then create and save the following g modules in it.
#fact.py
def factorial(n):
f = 1
for x in range(1,n+1):
f = f*x
return f
#sum.py
def add(x,y):
s = x+y
return s
#mult.py
def multiply(x,y):
s = x*y
return s
$\{\s4}$ − 在 package1 文件夹中创建并保存 $\{\s5\}$ 文件,其内容如下:
Step 2 − In the package1 folder create and save the init.py file with the following content.
#__init__.py
from fact import factorial
from sum import add
from mult import multiply
$\{\s6}$ − 在 package1 文件夹外创建以下 Jython 脚本,名为 $\{\s7\}$。
Step 3 − Create the following Jython script outside the package1 folder as test.py.
# Import your Package.
import package1
f = package1.factorial(5)
print "factorial = ",f
s = package1.add(10,20)
print "addition = ",s
m = package1.multiply(10,20)
print "multiplication = ",m
$\{\s8}$ − 从 Jython 提示符执行 test.py。将获得以下输出。
Step 4 − Execute test.py from Jython prompt. The following output will be obtained.
factorial = 120
addition = 30
multiplication = 200