65

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..."
4

2 回答 2

128
In [50]: import platform    
In [52]: platform.python_implementation()
Out[52]: 'CPython'
于 2013-02-05T22:25:19.993 回答
14

平台怎么样

它给了你

platform.python_implementation()
于 2013-02-05T22:26:32.857 回答