python<br>>>> coerce(3.1415, 6) # float and int<br>(3.1415, 6.0)<br>>>> results = coerce("a", 2.3)<br>Traceback (most recent call last):<br> File "<stdin>", line 1, in ?<br>TypeError: number coercion failed<br>
compile
将字符串编译为代码对象
compile(source, filename, mode)
python<br>>>> co = compile("0 and 'This' or 1 and 'that'", "-", "eval")<br>>>> exec(co)<br>>>> eval(co)<br>'that'<br>>>> co = compile("for x in range(10):\n\tprint x, ", "<console>", "single")<br>>>> exec(co)<br>0 1 2 3 4 5 6 7 8 9<br>
1.2 内置函数调用流程
graph TD;
A[选择函数] --> B{函数类型};
B -- 数值处理 --> C[调用 abs、cmp 等函数];
B -- 对象操作 --> D[调用 callable、delattr 等函数];
B -- 字符串处理 --> E[调用 chr、ord 等函数];
B -- 代码处理 --> F[调用 compile、eval 等函数];
2. 语句使用说明
Jython 中的语句用于控制程序的流程和结构。以下是一些常见语句的详细介绍。
2.1 部分语句介绍
语句
描述
语法
示例
assert
测试表达式是否为真,否则抛出异常
"assert" expression [, expression]
python<br>>>> a = 21<br>>>> assert a < 10<br>Traceback (innermost last):<br> File "<console>", line 1, in ?<br>AssertionError:<br>
break
终止内层循环并继续执行循环块后的代码
"break"
python<br>>>> for x in (1, 2, 3, 4):<br>... if x == 3:<br>... break<br>... print x<br>...<br>1<br>2<br>
class
开始类的定义
"class" name[(base-class-name(s))]:
python<br>>>> class test: # no base class<br>... pass # place holder<br>...<br>>>> t = test() # Calls class to create an instance<br>
continue
暂停当前循环块的执行,开始下一次迭代
"continue"
python<br>>>> for x in (1, 2, 3, 4):<br>... if x == 3:<br>... continue<br>... print x<br>...<br>1<br>2<br>4<br>
python<br>>>> a = "foo"<br>>>> print a<br>foo<br>>>> del a<br>>>> print a<br>Traceback (innermost last):<br> File "<console>", line 1, in ?<br>NameError: a<br>
2.2 语句执行流程
graph TD;
A[程序开始] --> B{遇到语句类型};
B -- 条件判断 --> C[执行 if、elif、else 语句];
B -- 循环控制 --> D[执行 for、while 语句];
B -- 异常处理 --> E[执行 try/except 语句];
B -- 函数与类定义 --> F[执行 def、class 语句];
B -- 变量操作 --> G[执行 del、global 语句];
python<br>>>> float(2) # int to float<br>2.0<br>>>> float(7L) # long to float<br>7.0<br>>>> float(abs(1.1 + 2.1j))<br>2.3706539182259396<br>>>> float("2.1") # string to float<br>2.1<br>
int
将参数转换为整数类型,可指定进制
int(x[, base])
python<br>>>> int(3.5) # float to int<br>3<br>>>> int(5L) # long to int<br>5<br>>>> int(abs(2.2 + 1.72j)) # complex to int<br>2<br>>>> int("012", 8) # octal string to int<br>10<br>>>> int("0x1A", 16) # hex string to int<br>26<br>
long
将参数转换为长整数类型,可指定进制
long(x, base)
python<br>>>> long(2.7) # float to long<br>2L<br>>>> long(abs(1.1 + 2.3j)) # complex to long<br>2L<br>>>> long("021", 8) # octal string to long<br>17L<br>>>> long("0xff", 16) # hex string to long<br>255L<br>
str
返回对象的字符串表示形式
str(object)
python<br>>>> str(1) # int to string<br>'1'<br>>>> str(4L) # long to string<br>'4'<br>>>> str(2.2 + 1.3j)<br>'(2.2 + 1.3j)'<br>>>> from java.util import Vector<br>>>> str(Vector)<br>'java.util.Vector'<br>>>> v = Vector()<br>>>> str(v)<br>'[]'<br>>>> v.add(type) # put a function in the vector<br>1<br>>>> str(v)<br>'[<java function type at 5229978>]'<br>
python<br>>>> # first use an anonymous dictionary for globals<br>>>> execfile("c:\\windows\\desktop\\testfile.py", {})<br>a b 7<br>>>> globals()<br>{'__name__': '__main__', '__doc__': None}<br>>>> # Now use current namespaces<br>>>> execfile("c:\\windows\\desktop\\testfile.py")<br>a b 7<br>>>> globals()<br>{'var2': 'b', 'var1': 'a', '__doc__': None, 'var3': 7, '__name__': '__main_'}<br>
open
打开指定文件并返回文件对象
open(filename[, mode[, buffering]])
python<br>>>> # open file in current directory for reading plus writing.<br>>>> fo = open("config.cfg", "r+")<br>>>> # Open file for binary reading<br>>>> fo = open("c:\soot\baf\Baf.class", "rb")<br>
5.2 异常处理与文件操作语句执行流程
graph TD;
A[程序遇到相关语句] --> B{语句类型};
B -- 异常处理 --> C[执行 try/except 语句];
B -- 文件执行 --> D[执行 execfile 语句];
B -- 文件打开 --> E[执行 open 语句];