Why is python not written in C++ ?

G

Gregory Ewing

Lawrence said:
at one
time there was an experiment to make the kernel compilable with a C++
compiler, without actually using any C++ features. The result: they lost
about 10% in speed. That was enough to put the kernel developers off taking
the experiment any further.

FWIW, certain parts of the Darwin kernel are written in a
carefully-selected subset of C++. So Apple evidently think
that it makes sense to use some C++ in a Unix kernel under
some circumstances.
 
A

Albert van der Horst

One thing that comes to mind is that it's much easier to distribute C
libraries than C++ libraries.

If I compile a main program with one C compiler and you compile a
dynamically loaded library with another C compiler on the same box, the
odds are pretty good they'll interoperate without any problems.

Not at all so with C++ compilers. The linkage is *way* more
complicated. Not just how the two compilers do name mangling, but how
they handle exceptions, RVO, and a zillion other details. Pretty much
the only way to make it work is to compile everything with exactly the
same compiler. That would make it pretty close to impossible for people
to take a Python core distribution and add their own extension modules
to it.

We had a similar discussion on comp.lang.forth.
The bottom line is that to implement a programming language
you want to use a simpler programming language, not a more
complicated one. Then there is a secondary requirement,
that language must allow sufficient access to low level features
to allow decent performance.
A third aspect is that Linux/Unices as well as Windows assume
OS interaction in terms of c-libraries. Any intermediary is
inconvenient at least.

Or in one catch phrase:
" To implement a programming language you need a high level
assembler like C."

(We went on whether Forth would be a suitable high level assembler
for Haskell. It would beat C++ -- not C -- for implementing Python,
that much I'm sure.)

Undoubtedly C is the right choice to implement Python.

Groetjes Albert
 
A

Albert van der Horst

Yes, there are a few corner cases where valid C syntax has different
semantics in C and C++. But, they are very few. Calling C++ a superset
of C is essentially correct.

One aspect of C++ is that all standard functions of C are called
in the same way. A large part of programs consists of stringing
API calls together. Those require little work to upgrade to
C++. So from a practical point of view this is very much true.
It is certainly correct from the level of a risk-averse development
manager deciding if he or she is willing to use C++ for the first time.
Fear of the unknown is a powerful deterrent. It's a lot easier to
accept something like C++ because "it's just a superset of C, and we've
been using C for years".

Even if it is overhyped, the C++ compiler vendors certainly tried
to make it happen.
I suspect the same effect contributed to Java's success as well. "Look,
it's got curly braces and semicolons. It's just like C!"

Of course!

Groetjes Albert
 
P

Paul Rubin

Albert van der Horst said:
We had a similar discussion on comp.lang.forth.

Heh, fancy meeting you here ;-)
The bottom line is that to implement a programming language
you want to use a simpler programming language, not a more
complicated one.

Nah, gas is written in C, and nobody implements VHDL as logic gates.
(We went on whether Forth would be a suitable high level assembler
for Haskell. It would beat C++ -- not C -- for implementing Python,
that much I'm sure.)

Haskell (or at least certain parts of it) should probably be implemented
in Coq or Agda, which are even higher level than Haskell.
Undoubtedly C is the right choice to implement Python.

Python has been experimentally implemented in Haskell:

http://hackage.haskell.org/package/berp-0.0.2

but the most interesting implementation (not yet ready for production,
but a serious ongoing project partly funded by the EU) is written in
Python itself:

http://codespeak.net/pypy/
 
L

Lawrence D'Oliveiro

FWIW, certain parts of the Darwin kernel are written in a
carefully-selected subset of C++. So Apple evidently think
that it makes sense to use some C++ in a Unix kernel under
some circumstances.

I wonder if that explains Apple’s well-known multitasking problems. :)
 
L

Lawrence D'Oliveiro

The bottom line is that to implement a programming language
you want to use a simpler programming language, not a more
complicated one.

That would rule out ever using a language to implement itself.
 
U

Ulrich Eckhardt

candide said:
Python is an object oriented langage (OOL). The Python main
implementation is written in pure and "old" C90. Is it for historical
reasons?

The fact that Python is OOP doesn't mean that the implementation of it has
to be written using an OOP language.

Other than that, I'm actually surprised that nobody mentioned that Python
actually _is_ written in C++. Yes, it's restricted to a subset thereof that
is compatible to C, but you could also claim that it was written in a
subset of C compatible to C++.

:)

Uli
 
C

Carl Banks

The fact that Python is OOP doesn't mean that the implementation of it has
to be written using an OOP language.

Other than that, I'm actually surprised that nobody mentioned that Python
actually _is_ written in C++. Yes, it's restricted to a subset thereof that
is compatible to C, but you could also claim that it was written in a
subset of C compatible to C++.

:)

I highly doubt the Python source would build with a C++ compiler.

C++ is "'mostly' 'backwards' compatible" with C insofar as you can
pretty easily write C code that is also legal (and semantically
equivalent) C++. But if you don't actively try to write code that is
compatible with both languages, chances are the C code will invoke one
of those "'minor' 'backwards' incompatibilies", the most common one
being failure to cast a pointer.


Carl Banks
 
U

Ulrich Eckhardt

Carl said:
I highly doubt the Python source would build with a C++ compiler.

As Christian showed, it doesn't. However, look around the sources a bit.
There are lots of places where e.g. the returnvalue of malloc() (or,
rather, the macro that resolves to something like it) is explicitly
type-cast to the according pointer type. When asked on the developers'
list, it was said that this was intended for compatibility with C++, e.g.
in cases where people want to embed Python into their C++ projects. Of
course, this contradicts Christian's statement that C++ compatibility
wasn't considered useful enough.

*shrug*


Uli
 
L

Lawrence D'Oliveiro

Christian said:
There isn't really a point in cluttering the source with type casts.

Makes you wonder why they bothered using a typed language at all.
 
C

Carl Banks

As Christian showed, it doesn't. However, look around the sources a bit.
There are lots of places where e.g. the returnvalue of malloc() (or,
rather, the macro that resolves to something like it) is explicitly
type-cast to the according pointer type. When asked on the developers'
list, it was said that this was intended for compatibility with C++, e.g.
in cases where people want to embed Python into their C++ projects. Of
course, this contradicts Christian's statement that C++ compatibility
wasn't considered useful enough.

I question why it needs to be compatible with C++ to be able to embed
Python in a C++ project. How many C++ compilers out there don't come
bundled with a C compiler?


Carl Banks
 
M

Martin v. Loewis

Am 10.08.2010 09:06, schrieb Ulrich Eckhardt:
As Christian showed, it doesn't. However, look around the sources a bit.
There are lots of places where e.g. the returnvalue of malloc() (or,
rather, the macro that resolves to something like it) is explicitly
type-cast to the according pointer type. When asked on the developers'
list, it was said that this was intended for compatibility with C++, e.g.
in cases where people want to embed Python into their C++ projects. Of
course, this contradicts Christian's statement that C++ compatibility
wasn't considered useful enough.

It doesn't contradict at all. In order to embed Python into a C++
application, it is sufficient if the Python header files can be compiled
with a C++ compiler, and linking to the C function works
properly - which is indeed the case.

People integrate Python with C++ quite often, both for embedding it
into C++, and for writing extension modules in C++.

Regards,
Martin
 
U

Ulrich Eckhardt

Martin said:
Am 10.08.2010 09:06, schrieb Ulrich Eckhardt:

It doesn't contradict at all. In order to embed Python into a C++
application, it is sufficient if the Python header files can be compiled
with a C++ compiler, and linking to the C function works
properly - which is indeed the case.

That's true, maybe I don't remember the exact rationale. Especially if even
someone like you, who is much deeper into Python development, doesn't, I'm
wondering if I'm misremembering something....

Cheers!

Uli
 
S

sturlamolden

That's true, maybe I don't remember the exact rationale. Especially if even
someone like you, who is much deeper into Python development, doesn't, I'm
wondering if I'm misremembering something....

Header (definition) and source (implementation) is not the same. A C++
compiler can use Python's header files and link with Python's C API
correctly. But it cannot compile Python's C source code. A C compiler
is required to compile and build Python.
 
A

Aahz

I also looked at Modula-3 once, and thought it had some real promise,
but I think it's probably deader than Ada now.

That's because you should be using Oberon instead.
 
A

Aahz

I'm not sure what the hiring issue is. I think anyone skilled in C++ or
Java can pick up Ada pretty easily. It's mostly a subset of C++ with
different surface syntax.

Heck, I learned Ada as a sixteen-year-old knowing only BASIC and Pascal.
 
L

Lawrence D'Oliveiro

In message
A C++ compiler can use Python's header files and link with Python's C API
correctly. But it cannot compile Python's C source code. A C compiler
is required to compile and build Python.

Since when do you find a C++ compiler not accompanied by a C one?
 
G

Grant Edwards

Heck, I learned Ada as a sixteen-year-old knowing only BASIC and Pascal.

Unfortunately, there just aren't that many Aahzes to hire (sixteen
years old or otherwise), and of the non-Aahz progrommers out there
it's shocking how many of them them are apparently incapable of
learning a second language.

Regardless of how easy something is to learn, management always wants
to hire people who don't have to.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,781
Messages
2,569,616
Members
45,306
Latest member
TeddyWeath

Latest Threads

Top