[Python-ideas] Add list.join() please

Steven D'Aprano steve at pearwood.info
Wed Jan 30 05:57:52 EST 2019


On Wed, Jan 30, 2019 at 11:07:52AM +0100, Jamesie Pic wrote:

> What do you think could be the developer intent when they do
> ",".join([2, "2']) ?

I don't know what your intent was, although I can guess, but I do know 
that I sure as hell don't want a dumb piece of software like the 
interpreter running code that I didn't write because it tried to guess 
what I possibly may have meant.

http://www.catb.org/jargon/html/D/DWIM.html

And from the Zen:

Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.

Don't think about toy examples where you put literals in the code. 
Sure, we want a string, but that's trivial. What sort of string and what 
should it look like? Think about non-trivial code like this:

    header = generate_header()
    body = template.format(','.join(strings))
    document = make(header, body)

and imagine that somehow, a non-string slips into something which is 
supposed to be a string. Now what do you think my intent is?

It isn't enough to just say "I want a string dammit, and I don't care 
what's in it!".

If a non-string slips in there, I sure as hell want to know how and why, 
because that's a bug, not a minor inconvenience.

The most junior developer in the team could easily paper over the bug by 
adding in a call to map(str, strings) but that doesn't fx the bug, it 
just hides it and all but guarantees the document generated is corrupt, 
or worse, wrong.

    "I find it amusing when novice programmers believe their
     main job is preventing programs from crashing. ... More
     experienced programmers realize that correct code is
     great, code that crashes could use improvement, but
     incorrect code that doesn’t crash is a horrible nightmare."
     -- Chris Smith

If we look at where the strings come from:

    strings = [format_record(obj) for obj in datasource if condition(obj)]

we're now two steps away from the simplistic "we want a string" 
guess of your example. When we look at format_record and find this:

    def format_record(record):
        if record.count < 2:
            ...
        elif record.type in ('spam', 'eggs'):
            ...
        elif record.next() is None:
            ...
        # and so on for half a page


we're just getting further and further away from the trivial cases of 
"just give me a string dammit!".

Going back to your example (correcting the syntax error):

    ",".join([2, "2"])


To save you about a quarter of a second by avoiding having to type quote 
marks around the first item, you would cost me potentially hours or days 
of hair-tearing debugging trying to work out why the document I'm 
generating is occasionally invalid or corrupt in hard to find ways.

That's not a trade off I have any interest in making.



-- 
Steve


More information about the Python-ideas mailing list