101

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 the answer should be supported by historical references (and not opinion based).

Christophe
  • 74,672
  • 10
  • 115
  • 187
Piotr Dobrogost
  • 1,145
  • 2
  • 8
  • 10
  • 10
    I don`t know why, but I suspect something close to this : http://thread.gmane.org/gmane.comp.version-control.git/57643/focus=57918 :) – Matthieu Nov 23 '10 at 21:45
  • Thanks for the link. I've never seen a rant like that from Linus Torvalds before. BTW, I agree completely. – Larry Coleman Nov 23 '10 at 21:52
  • 13
    @Larry Coleman: Never seen Linus rants? You must be avoiding "the internets"... >_> – dr Hannibal Lecter Nov 23 '10 at 22:02
  • 18
    @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
  • 2
    @Matthieu, I believe you meant to post that as an answer ;) – erjiang Nov 23 '10 at 22:07
  • 2
    I wonder what Linus thinks of Java :D – Jeremy Nov 24 '10 at 01:40
  • 4
    @Jeremy, more importantly, I wonder what Java would think of Linus? – Tim Post Nov 24 '10 at 05:38
  • 2
    @PiotrDobrogost To be fair, that particular rant should be judged against state of C++, it's compilers and libraries, in 2007. – hyde Apr 01 '13 at 21:39
  • 5
    Well, this is a response to Linus' rant, what about this : http://warp.povusers.org/OpenLetters/ResponseToTorvalds.html – avi Jun 06 '13 at 18:34
  • 1
    I've actually tried re-implementing Python in C++ a couple times now. Some things like the reference counted objects scream `shared_ptr` and most of the macros could be replaced with `inline`/`constexpr` functions. Plus, C++ allocators would be an eloquent (if not interesting) solution to the Python memory model. The reality is that Python's source code is gorgeous and there are only a few places where performance could be improved. Plus, it has great exposure. These days, I am happy using Boost's Python library to build my extensions and I leave the interpreter alone. – Travis Parks Jun 12 '13 at 18:46
  • @hyde And also against C++ programmers at the time :P – Andres F. Jun 12 '13 at 18:46
  • 6
    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
    The other question is "why isn't python written in python?". I know that the first version can't, but all subsequent could be very well. – ott-- Jun 12 '13 at 19:33
  • 1
    @PiotrDobrogost That's a strawman. It's obvious why you wouldn't want to write modern, non-performance critical code in assembly. Your question is different though: "why C instead of C++?". Let me reverse the question: "why C++ instead of C?". Or worse: "why C++ and not Pascal. I like Pascal more!" – Andres F. Jun 12 '13 at 20:11
  • 1
    @ott Indeed, _that_ question does make sense: why isn't the standard implementation of Python written in Python? It follows that the designer of Python likes his own language, so the question is interesting. – Andres F. Jun 12 '13 at 20:14
  • 2
    @Jeremy *"I wonder what Linus thinks of Java :D"* - Given that he seems to have mistaken *C++* for *Java*, probably the same. – Christian Rau Jun 13 '13 at 15:47
  • Read [this](http://www.velocityreviews.com/forums/t729767-p11-why-is-python-not-written-in-c.html)? Looks very closely related to this question. And, [this](http://www.mail-archive.com/python-list@python.org/msg290862.html) discussion at Python list asks the very same question. – vpit3833 Nov 23 '10 at 21:54
  • @PiotrDobrogost, That's **hardly anything at all compared** to his other rants. Is that the only Linus rant you have read? – Pacerier Feb 16 '17 at 19:34

3 Answers3

143

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.

rchard2scout
  • 754
  • 4
  • 10
Jerry Coffin
  • 44,385
  • 5
  • 89
  • 162
  • 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. – fkl Dec 05 '13 at 06:44
  • 1
    "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 05:43
  • So is Python essentially a software written in C (not the interpreters itself)? Is that what is meant by "Python implemented in C"? I'm trying to understand the difference in "Java being a derivative or C" and "Python being written in C". I understand that the certain JVMs are written in C/C++ – Honinbo Shusaku Jul 09 '16 at 08:58
32

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.

Jörg W Mittag
  • 101,921
  • 24
  • 218
  • 318
  • 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
  • @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
    +1 for mentioning the ABI – jk. Jun 13 '13 at 11:12
  • So is Python essentially a software written in C (not the interpreters itself)? Is that what is meant by "Python implemented in C"? I'm trying to understand the difference in "Java being a derivative or C" and "Python being written in C". I understand that the certain JVMs are written in C/C++ – Honinbo Shusaku Jul 09 '16 at 08:56
  • 3
    @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 09 '16 at 14:28
  • @JörgWMittag Sorry but you're wrong on this one. The specification of Python is CPython. – Miles Rout Oct 18 '17 at 11:17
  • 4
    @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
  • 4
    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
  • @JörgWMittag You're mistaken. There is no specification. No specification document exists anywhere. The Python *documentation* is very clear that it is a *description* of Python and not a *prescription* or *specification*. CPython defines Python. There's no specification for Python anywhere. There is a reference implementation. This is common. The same is true of Ruby and Perl (pre-6). – Miles Rout Oct 19 '17 at 06:29
  • 1
    @MilesRout: So, Guido van Rossum is wrong then, when he says that Reference Counting is not part of the specification, even though CPython is the specification and uses Reference Counting? Only one of the two statements "CPython is the specification" and "Reference Counting is not part of the specification" can be true. – Jörg W Mittag Oct 19 '17 at 07:30
  • 1
    @JörgWMittag Again, there is no specification. Show me a link and I'll change my mind (you will struggle to do so). – Miles Rout Oct 19 '17 at 07:34
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.

Larry Coleman
  • 6,101
  • 2
  • 25
  • 34
  • 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. http://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 01: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