68

In Python's tutorial one can read that Python's original implementation is in C;

On the other hand, the Python implementation, written in C, (...)

I'm very curious why was Python written in C and not C++? I'd like to know the reasoning behind this decision and I'm looking for references to historic data.

closed as primarily opinion-based by yannis May 20 '15 at 10:05

Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question.

110

From everything I've seen, it's a combination of practical and historical reasons. The (mostly) historical reason is that CPython 1.0 was released in 1989. At that time, C was just recently standardized. C++ was almost unknown and decidedly non-portable, because almost nobody had a C++ compiler.

Although C++ is much more widespread and easily available today, it would still take a fair amount of work to rewrite CPython into the subset of C that's compatible with C++. By itself, that work would provide little or no real benefit.

It's a bit like Joel's blog post about starting over and doing a complete rewrite being the worst mistake a software company can make. I'd counter that by pointing to Microsoft's conversion from the Windows 3.0 core to the Windows NT core, and Apple's conversion from MacOS 9 to Mac OS/X. Neither one killed the company -- but both were definitely large, expensive, long-term projects. Both also point to something that's crucial to success: maintaining both code bases for long enough that (most) users can switch to the new code base at their leisure, based on (at least perceived) benefits.

For a development team the size of Python's, however, that kind of change is much more difficult. Even the change from Python 2 to 3 has taken quite a bit of work, and required a similar overlap. At least in that case, however, there are direct benefits to the changes, which rewriting into C++ (by itself) wouldn't (at least immediately) provide.

Linus Torvalds's rant against C++ was brought up, so I'll mention that as well. Nothing I've seen from Guido indicates that he has that sort of strong, negative feelings toward C++. About the worst I've seen him say is that teaching C++ is often a disaster -- but he immediately went on to say that this is largely because the teachers didn't/don't know C++.

I also think that while it's possible to convert a lot of C code to C++ with relative ease, that getting much real advantage from C++ requires not only quite a bit more rewriting than that, but also requires substantial re-education of most developers involved. Most well-written C++ is substantially different from well-written C to do the same things. It's not just a matter of changing malloc to new and printf to cout, by any stretch of the imagination.

  • 2
    +1 You quote a lot; they're interesting. It seems it would be even better if links could be added. – n611x007 Nov 25 '12 at 22:42
  • 1
    Just submitted an edit with a link to Joel's blog post on rewrites joelonsoftware.com/articles/fog0000000069.html – MarkJ Jun 13 '13 at 8:33
  • This was an excellent answer. I learned a lot from it. – Games Brainiac Jun 22 '13 at 16:37
  • 1
    +1 specifically for mentioning the c which can be ported to c++ with relative ease is probably not worth doing. I knew this already for a long time but the answer really strengthened point of view plus added several dimensions to look at it. – fayyazkl Dec 5 '13 at 6:44
  • "Apple's conversion from MacOS 9 to Mac OS/X" note that OS/X is not a rewrite from scratch: it was rather a switch from MacOS9 to NeXTStep, improved and rebranded for Apple – Jivan Dec 29 '14 at 5:43
28

I think the reason why it was originally written in ANSI C89 is quite simply because back then, C++ was just not a workable choice what with incompatibilities between different compilers and such. I mean, it took until, what was it, 2005, to come up with an ABI specification that would allow code compiled with one compiler to call code compiled with a different compiler?

The more interesting question is why it is still written in C89.

And there is a surprising answer: because people actually use Python on platforms for which no C++ and no C99 compiler exists! When the Forth-inspired threaded-code interpreter optimizations were merged, there was a huge discussion about it, because the code (necessarily) used computed goto which is not a part of C89. There were apparently real fears that this feature might not be available on some of the platforms that Python is currently used on.

The same thing happened with Unladen Swallow, wich uses LLVM, which is written in C++. It was made very clear that a requirement for merging Unladen Swallow into CPython would be that you can compile it without the JIT compiler, since there are platforms people run Python on, for which no C++ compiler exists.

Of course, nowadays, CPython is no longer the only Python implementation. There is PyPy, which is written in RPython (a statically typed subset of Python), Jython in Java, IronPython in C#, Pynie in NQP and PIR and so on.

  • 3
    I'm half tempted to upvote this, but I know of no such platform where a C++ compiler does not exist (Particularly given that Comeau C++ compiles to C) – Billy ONeal Feb 14 '11 at 23:58
  • 1
    +1 for mentioning the ABI – jk. Jun 13 '13 at 11:12
  • 2
    @Abdul: No, Python is not a software at all. It is a specification. There are multiple implementations of that specification, written in multiple languages. IronPython is written in C♯, Jython in Java, PyPy in RPython, Pynie in NQP, PIR, and Perl6, Pyston in C++, CPython in C. The statement "Python is written in C" doesn't make sense. Python is not a software. It is a specification. It is written in English, not in any programming language. "Java is a derivative of C" is mainly wrong. Java is inspired by Objective-C, but it gets rid of most of the C parts and takes mostly the Smalltalk parts. – Jörg W Mittag Jul 9 '16 at 14:28
  • 2
    @MilesRout: There are multiple cases where the specification deviates from CPython. For example: the Python specification does not guarantee deterministic finalization, but CPython does, at least for non-circular references. But even though CPython guarantees deterministic finalization for non-circular references, writing code which relies on that fact is broken, since it is not part of the spec. (I can't find the quote right now, but GvR has explicitly said that deterministic finalization and reference counting are private internal implementation details of CPython.) – Jörg W Mittag Oct 18 '17 at 11:58
  • 2
    Similarly, CPython guarantees that two Python threads cannot execute in parallel, but that, too, is a private internal implementation detail of CPython and not guaranteed by the language spec. If what you say were true, there couldn't possibly be any other implementations, since any alternative implementation must necessarily behave identical to CPython, and thus must necessarily be identical. (Modulo refactorings that don't change observable behavior.) – Jörg W Mittag Oct 18 '17 at 12:02
9

A better question might be: "Why isn't Python written in Python?"

More to the point, once enough primitives for Python classes and objects are written in C, those can be used for writing the rest of the interpreter, so you wouldn't gain anything by using C++ instead.

  • 1
    If you follow the first link in my answer, you will see a reference to an implementation of Python in Python. That is not production ready yet. That is funded by the EU. codespeak.net/pypy/dist/pypy/doc is the link if it is difficult to find out from my answer. – vpit3833 Nov 23 '10 at 22:02
  • 2
    This is actually a pretty deep answer. Not that Guido's Python is literally written in Python but that the low level structures in C are used to write higher-level ones. – Jeremy Nov 24 '10 at 1:43
  • 1
    I think you miss the point as there's quite a difference (for people working on the interpreter itself) what language the interpreter is written in. The language influences how these primitives look like and how they interact with each other. For instance now, in C implementation of Python, one has to remember to increment and decrement reference counts manually whereas it might be possible to use smart pointers in C++ for this. – Piotr Dobrogost Jul 28 '14 at 14:06
  • Now that PyPy is available and interestingly outperforms CPython sometimes, it would have been such a great idea I think. – Sai Kumar Battinoju Jul 20 '18 at 11:41

Not the answer you're looking for? Browse other questions tagged or ask your own question.