Explanation of this Python language feature? [x for x in x for x in x] (to flatten a nested list)

Chris Angelico rosuav at gmail.com
Sat Apr 5 01:55:42 EDT 2014


On Sat, Apr 5, 2014 at 4:23 PM, Mark H Harris <harrismh777 at gmail.com> wrote:
> The only advantage of C++ over C is polymorphism, really. There are in my
> view only three reasons to even use C++: 1) the iostream library, and 2)
> polymorphism, and 3) operator overloading. If you need to do all three, then
> C++ is a really good candidate.

The iostream library actually gives you very little over the stdio
functions (printf, FILE*, etc), beyond that they're arguably easier to
use. (I say "arguably" because there've been plenty of times when I've
been writing C++ code and just not bothered with cout, finding printf
the better option. Sometimes you find yourself arguing with cout and
it's not worth arguing with.)

Operator overloading, ultimately, is just this:

x + y
// becomes
x.operator+(y)
// or
operator+(x,y)

When you're actually writing C++ code, that's a huge advantage in
readability. But if you're writing an interpreter for another
language, there's no benefit; you may as well not bother. Maybe it'd
be of value if you want to write a Python-to-C++ translator that then
lets you compile the resulting C++ code, but only if you want the C++
code to be readable.

So all you're left with is polymorphism. Well, big problem: Python and
C++ have distinctly different semantics for multiple inheritance. It
wouldn't be possible, much less practical, to try to implement
Python's MRO on top of a C++ class structure, other than by basically
ignoring the whole structure and using it much the same way PyObject *
is used in the existing C code.

> I am still thinking about the concept of unifying Number; Number as a C++
> abstract base class, and an entire Class hierarchy which carries through
> making *any* Number just work. The ability of the C++ compiler to construct
> and maintain the virtual function tables would be an advantage. Operator
> overloading (and maybe templates) would make C++ advantageous also.

The virtual function tables don't cater for the MRO, see above. But
even with simple single inheritance, the effort of creating a new
class at run-time would be quite a problem; remember, 'class' in C++
is a declaration to the compiler, but 'class' in Python is an
executable statement.

> Guido told me that the modern C python is object oriented. Operator
> overloading is a big part of this. It seems to me that a modern object
> oriented language would best be implemented with a true object oriented base
> language, C++ rather than C.  I have always questioned this, just curious
> why the decision for C was made--- historically, methodologically, and maybe
> scientifically.

Python is object oriented, and it has operator overloading. But it's
possible to implement operator overloading in a language that doesn't
have it - it's not OOPs all the way down, turtle-style - so somewhere
there has to be that boundary, and building one object oriented
language on top of another doesn't necessarily actually give many
benefits.

ChrisA



More information about the Python-list mailing list