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..."
答案 0 :(得分:69)
In [50]: import platform
In [52]: platform.python_implementation()
Out[52]: 'CPython'
答案 1 :(得分:9)