Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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.

share|improve this question
9  
I don`t know why, but I suspect something close to this : thread.gmane.org/gmane.comp.version-control.git/57643/… :) –  Matthieu Nov 23 '10 at 21:45
11  
@Larry Coleman: Never seen Linus rants? You must be avoiding "the internets"... >_> –  dr Hannibal Lecter Nov 23 '10 at 22:02
14  
@Larry I did see this rant and lost almost all respect for Linus after reading it. Shame on him. –  Piotr Dobrogost Nov 23 '10 at 22:04
4  
Well, this is a response to Linus' rant, what about this : warp.povusers.org/OpenLetters/ResponseToTorvalds.html –  avi Jun 6 '13 at 18:34
3  
I fail to see the point of asking "why is (popular program) written in (language X) and not (language Y)?". Or rather, the same question can be reversed: why Y and not X? –  Andres F. Jun 12 '13 at 18:49

3 Answers 3

up vote 63 down vote accepted

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.

share|improve this answer
1  
+1 You quote a lot; they're interesting. It seems it would be even better if links could be added. –  naxa Nov 25 '12 at 22:42
    
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

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.

share|improve this answer
1  
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
    
@Billy ONeal: Does Comeau C++ compile to C89? –  Giorgio Jun 12 '13 at 19:24
    
@Giorgio: As far as I know. –  Billy ONeal Jun 12 '13 at 19:29
    
+1 for mentioning the ABI –  jk. Jun 13 '13 at 11:12

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.

share|improve this answer
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
1  
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
    
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 at 14:06

protected by gnat Mar 23 at 14:18

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

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