>>> print inspect.getsource(gcd)
def gcd(a, b):
"""Calculate the Greatest Common Divisor of a and b.
Unless b==0, the result will have the same sign as b (so that when
b is divided by it, the result comes out positive).
"""
while b:
a, b = b, a%b
return a
从Python 3.5开始,gcdis in the ^{} module;不推荐使用fractions中的一个。此外,inspect.getsource不再返回任何方法的解释性源代码。
使用m-n的算法可以运行非常长的时间。
这个表现更好:
这个版本的代码使用Euclid算法来查找GCD。
是in the standard library。
Python 2.7中
inspect
模块的源代码:从Python 3.5开始,} module ;不推荐使用
gcd
is in the ^{fractions
中的一个。此外,inspect.getsource
不再返回任何方法的解释性源代码。相关问题 更多 >
编程相关推荐