将函数作为参数传入。
def conv(func,wa):
try:
return [func(item) for item in wa]
except (ValueError,TypeError),digs:
print digs
wb=(12,34.3,-23,-6.2e8,9999999999L)
print conv(int,wb)
print conv(float,wb)
print conv(long,wb)
运行后返回
[12, 34, -23, -620000000, 9999999999L]
[12.0, 34.299999999999997, -23.0, -620000000.0, 9999999999.0]
[12L, 34L, -23L, -620000000L, 9999999999L]
定义一个通用函数接口,每次只是将不同的函数传入,即可达到想要的结果。
自己感觉这里非常不错,但是还没有完全吃透里边的思想,只是有了一丝灵感,先记录下来等将来慢慢体会