[Python-ideas] Proposal: Tuple of str with w'list of words'

Steven D'Aprano steve at pearwood.info
Tue Dec 6 20:03:46 EST 2016


On Tue, Dec 06, 2016 at 04:01:24PM -0500, Random832 wrote:
> On Sat, Nov 12, 2016, at 13:05, Steven D'Aprano wrote:
> > I'm rather luke-warm on this proposal, although I might be convinced to 
> > support it if:
> > 
> > - w'...' unconditionally split on any whitespace (possibly 
> >   excluding NBSP);
> > 
> > - and normal escapes worked.
> 
> Is there any particular objection to allowing the backslash-space escape
> (and for escapes that mean whitespace characters, such as \t, \x20, to
> not split, if you meant to imply that they do)?

I hadn't actually considered the question of whether w-strings should 
split before, or after, applying the escapes. (Or if I had, it was so 
long ago that I forgot what I decided.) I suppose there's no good reason 
for them to apply before splitting. I cannot think of any reason why you 
would write:

    w"Nobody expects the Spanish\x20Inquisition!"

expecting to split "Spanish" and "Inquisition!". It's easier to just 
press the spacebar. So let's suppose that escapes are processed after 
the string is split, so that the w-string above becomes:

    ['Nobody', 'expects', 'the', 'Spanish Inquisition!']


Do we still need a new "\ " escape for a literal string? We clearly 
don't *need* it, since the user can write \x20 or \o40 or even 
'\N{SPACE}'. I'm *moderately* against it, since its hard to spot escaped 
spaces in a forest of unescaped ones, or vice versa:

    # example from the OP
    songs = w'My\ Bloody\ Valentine Blue\ Suede\ Shoes'

I think that escaping spaces like that will be an attractive nuisance. I
had to read the OP's example three times before I noticed that the space
between Valentine and Blue was not escaped.

What about ordinary strings? What is 'spam\ eggs'? It could be:

- allow the escape and return 'spam eggs', even though it is pointless;

- disallow the escape, and raise an exception, even though that's 
  inconsistent with w-strings.

I'm not really happy with either of those solutions (although I'm 
slightly less unhappy with the first). So in order of preference, least 
to worst:


strong opposition -1 to the original proposal of w-strings with no 
escapes except for \space;

weak opposition -0.25 for w-strings where \space behaves differently 
(raises an exception) in regular strings;

mildly negative indifference -0 for w-strings with \space allowed in 
regular strings as well;

mildly positive approval +0 for w-strings without bothering to allow 
\space at all (the user can use \x20 or equivalent).


For the avoidance of doubt, by \space I mean a backslash followed by a 
literal space character.


> That would provide the extra push to this being beneficial over split().

True, but it's not a lot of extra value over split().

If Python had this feature, I'd probably use it, but since it doesn't, I 
cannot in fairness ask somebody else to do the work on the basis that it 
is needed. I still think the existing solutions are Good Enough:

- use split when you don't have space in any term: "fe fi fo fum".split()

- use a list of manually split terms when you care about spaces:
  ['spam and eggs', 'cheese', 'tomato']




> I also have an alternate idea: sl{word1 word2 'string 3' "string 4"}

Why "sl"?

That looks like a set or a dict. Its bad enough that w-strings return a 
list, but to have "sl-sets" return a list is just weird :-)



-- 
Steve


More information about the Python-ideas mailing list