Jython 函数编程全解析
1. Jython 函数基础
在 Jython 里,函数属于可调用对象,既能够独立于类存在,也能在类定义中以方法的形式呈现。Jython 允许函数作为一等对象,可动态创建,还能当作参数传递或者作为返回值返回。
1.1 函数定义
Jython 函数的定义语法如下:
"def" function__name([parameter_list])":"
code block
函数名可以是任意不含空格的字母数字字符串,但首字符得是字母或者下划线 _
,且名称大小写敏感。使用时要留意避免与其他函数(像内置函数)重名。例如:
>>> S = "A test string"
>>> type(S) # test the built-in type()
<jclass org.python.core.PyString at 6879429>
>>> def type(arg):
... print "Not the built-in type() function"
... print "Local variables: ", vars() # look for "arg"
...
>>> type(S)
Not the built-in type() function
Local variables: