Compare Python Implementations without Assuming CPython Internals
Core idea
CPython, PyPy and other implementations aim to implement Python semantics but differ in object representation, garbage collection, optimization and native interoperability. Detect the implementation using sys.implementation.name or platform.python_implementation. Check capability and version explicitly; avoid using CPython bytecode or refcount behavior as portable contracts.
import platform,sysprint(platform.python_implementation())print(sys.implementation.name)These are observed local labels; PyPy and other runtimes return different ones.
Expected output:
CPythoncpythonTest a portable value contract
a=[3,1,2]b=sorted(a)print(a,b)print(10**30+1>10**30)Public sorting and integer semantics are suitable cross-implementation tests, independent of memory layout.
Expected output:
[3, 1, 2] [1, 2, 3]TrueCommon mistake
Do not assume every extension compiled for CPython imports into PyPy or other runtimes. Check the dependency’s published support and test actual targets.
Practice
List three language-level tests and three CPython-only observations; design a matrix for CPython and a separately installed alternative runtime.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Alternative implementations
Check your understanding of alternative implementations.
What is printed?
import sysprint(sys.implementation.name)What is printed?
print(sorted([2,0,1]))Which is implementation-specific?
How establish alternative runtime support?