最佳答案
Python 有几种不同的实现: CPython、 Jython、 PyPy 等等。我想以编程的方式确定我的代码在哪个实现上运行。我该怎么做?
具体来说,为我编写一个名为 get_implementation_name()
的函数:
impl_name = get_implementation_name()
if impl_name == "CPython":
print "I can abuse CPython implementation details. (I'm a bad, bad man.)"
elif impl_name == "PyPy":
print "Can't count on reference-counting garbage collection here..."
else:
print "I better be careful..."