Python有文档字符串(docstrings)
文档字符串是附加到模块、函数和方法的字符串,它有助于创建帮助命令和自动化文档。
def frobnicate(bar):
"""frobnicate takes a bar and frobnicates it
>>> bar = Bar()
>>> bar.is_frobnicated()
False
>>> frobnicate(bar)
>>> bar.is_frobnicated()
True
"""
Ruby的与Javadocs类似,它们可以在运行时使用1.9的“方法#源位置”从文件检索。
Python有更多的库
Python有大量的模块和绑定的库。
Python有多重继承
Ruby没有多重继承。它作为抽象类类型重复使用模块。
Python有列表/字典理解能力
Python:
res = [x*x for x in range(1, 10)]
Ruby :
res = (0..9).map { |x| x * x }
Python:
>>> (x*x for x in range(10))
at 0xb7c1ccd4>
>>> list(_)
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
Ruby:
p = proc { |x| x * x }
(0..9).map(&p)
Python 2.7+:
>>> {x:str(y*y) for x,y in {1:2, 3:4}.items()}
{1: '4', 3: '16'}
Ruby:
>> Hash[{1=>2, 3=>4}.map{|x,y| [x,(y*y).to_s]}]
=> {1=>"4", 3=>"16"}
Python有装饰器
与Ruby中创建的装饰器类似,但Ruby中的装饰器不像Python中那样重要,它是非必需的。