您的位置:首页 > 其它

语言的口味(taste)

2015-12-01 11:05 204 查看
这里的
taste
有两层意思,第一层表示语言本身的偏好(preference,倾向性)、特征、特色;第二层表示本人对这些编程语言的偏爱。

C-Style vs Python-Style

所谓风格,就是语言的推荐做法,也即该做法能充分调用语言的优势。Python自然是强大的
list comprehension
,如下所示当对tuple(或者list)的索引同步时,自然可以使用zip对象,统一索引。

# Python Style
out_shape=[
(ishp+tsp)*tshp-tsp
for ishp, tshp, tsp in zip(image_shape, tile_shape, tile_spacing)
]


# C Style
out_shape = [0, 0]
out_shape[0] = (image_shape[0]+tile_spacing[0])*tile_shape[0]-tile_spacing[0]
out_shape[1] = (image_shape[1]+tile_spacing[1])*tile_shape[1]-tile_spacing[1]


python

prefer
“clear and obvious” to “compactness of expression”:清楚的表达的重要性甚于紧凑的表达(除非效率有显著的提升)。正是基于此,Guido(python语言的设计者)把
reduce
这一和
map
平行的经常成对出现的内置函数,放到了
functionals
模块中。对此,他的解释是这样的:

almost every time I see a reduce() call with a non-trivial function argument, I need to grab pen and paper to diagram what’s actually being fed into that function before I understand what the reduce() is supposed to do.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: