Is this a dream or a nightmare? (Was Re: XML)

Alex Martelli aleaxit at yahoo.com
Sun Oct 8 06:17:57 EDT 2000


"David T. Grove" <pete at petes-place.com> wrote in message
news:39dfa617.25953118 at news.davesworld.net...
    [snip]
> >> > a good average for comparison is 1 line of perl for every 25 lines of
C,
> >> > TclTk, Python, or Visual Basic, and every 100 lines of C++.
> >
> >Such an assertion can only come from a mind that's totally out of
> >synch with reality.  400% *more* lines of code for C++ than for
>
> OOP over a procedural language has more lines, no matter how you look

C++ isn't "OOP over a procedural language".  It's a multi-paradigm
language, with procedural, object, and generic programming, all
supported quite reasonably, and melding rather synergically.

C is _just_ procedural -- period.  Anything else, you have to build
up from scratch, or do without -- and you don't get fundamental
language tools to help you build, or help you use what you build.

Just std::string and std::vector, by themselves, are A LOT of
excellent infrastructure code, enough to slash development time
(and resulting SLOC) by *a lot*; and std::map is not far behind.

> >C?!  That's even crazier than the 2500%-more assertion for Python
    [snip]
> Granted, python is very close to Perl in these respects, but it's
> still more verbose.

Yes, but *NOT* by _anywhere_ like TWENTYFIVE TIMES, which is
what your "lumping" brought you to ludicrously claim.

> die unless @d = map {"$basedir/$_"} grep {!-d && -w} readdir(dir);
>
> ...which is perfectly logical and readable to me as a single
> "thought"... "get me all the filenames in that directory or halt
> immediately because you'll screw something up". Single thoughts don't

Even for such an extreme case, idiomatic Python, while less concise,
is of course nothing like TWENTYFIVE TIMES more.  E.g.:
    entries = [ os.path.join(basedir,file) for file in os.listdir(basedir) ]
    allfiles = [ f for f in entries if os.access(f,os.W_OK) and
os.path.isfile(f) ]
    if not allfiles: raise ValueError, "no writable plainfiles in directory"

A ratio of about 2 to 4 (for "worst-case"/"extreme" examples) is indeed
quite a defensible hypothesis.  That's a *far sight* from TWENTYFIVE, in
case you had not noticed.


> Without the newbies, you can't match it with any python that I'm aware

Assignment not being acceptable within an expression, I do believe you
need two statements, not one, to set something AND raise an exception
if the value you've just set is empty.  Computing the "allfiles" value in
one Python statement is of course trivial (you do not HAVE to name the
intermediate 'entries' value -- you can just use the list comprehension
expression for it, embedded inside the one for allfiles); but in Python's
normal approach, such super-concision takes a second seat to clarity.

If the idiom "set this, but raise exception if empty" is common in your
code, say it happens 10 times in one script, then you will of course
have towards the start of it a single function definition such as:
    def nonempty(value, exception=ValueError, msg="empty list!"):
        if not value: raise exception, msg
        return value
and you will amortize these three lines over several uses such as:
    resultlist=nonempty([x for x in f if foo(x)], msg="no good x!")
thus taking the "irreducible ratio" (amortized) from about 2 to, say,
about 1.3 (and the "typical idiomatic ratio", similarly amortized, from
about 3 to about 2.3).

Again: nothing like 25!

> of. But then, I was also comparing VB and C in there, and I challenge
> anyone to do that in less than 25 lines of C or VB (1 line = 1
> semicolon per line, no cheating)

I don't particularly care about either C or VB.  In standard C++, you
have no built-in way to take a directory listing, nor to test if a given
file is a directory or a plain-file (functionalities not in the standard
library).  Assuming you've #include'd suitable headers supplying such
functionality, I predict about 12 lines for a good, typical, idiomatic
implementation -- std::copy_if to build up a std::vector of those
files that satisfy the condition, a few lines to objectify that condition,
the test-if-empty-then-raise-exception, plus declarations, braces &c.


> >vs Perl.  If this quote is correct, then the site must clearly be one
> >devoted to silly humour.
>
> Bah. Be nice and pay attention. You just totally zapped the entire
> meaning of this thread. I'm not python-bashing... quite the opposite
> if you'd take time to read up a little in the thread.

I don't particularly care, in this context, whether your purpose is
to bash Python or to praise it.  I do care that the absurd numbers
you spewed, about SLOC ratios between languages, don't go
unchallenged -- wouldn't want some poor innocent readers to
believe there's anything defensible about them!


Alex






More information about the Python-list mailing list