semi-concatenated strings

Rich Harkins rharkins at thinkronize.com
Fri May 31 12:39:07 EDT 2002


On Friday 31 May 2002 11:21 am, Grant Griffin wrote:
> In article <mailman.1022791249.9251.python-list at python.org>, Skip says...
>
> [ cool string concatenation method snipped ]
>
> I guess I don't see what's so bad about having to put a "+" at the end of
> each--except maybe that it brings on the need for a continuation backslash:
>
>     rows = self.executesql("select cities.city, state, country"          +\
>                            "    from cities, venues, events, addresses"  +\
>                            "    where     cities.city like %s"           +\
>                            "          and events.active = 1"             +\
>                            "          and venues.address = addresses.id" +\
>                            "          and addresses.city = cities.id"    +\
>                            "          and events.venue = venues.id",
>                            (city,))
>
> Sure, it's a little more typing, but one could argue that its easier to
> read because it's explicit.  (My non-SQL-trained eye would have initially
> read the original as a series of parameters separated by commas which, upon
> closer inspection, would be found not to actually be there.)
>
> Better yet would be if the parser would automatically concatenate
> incomplete expressions (as identified by a line that ends with an
> operator), much as it automatically concatenates incomplete list and
> dictionary initialization statements.  Then your example would become:
>
>     rows = self.executesql("select cities.city, state, country"          +
>                            "    from cities, venues, events, addresses"  +
>                            "    where     cities.city like %s"           +
>                            "          and events.active = 1"             +
>                            "          and venues.address = addresses.id" +
>                            "          and addresses.city = cities.id"    +
>                            "          and events.venue = venues.id",
>                            (city,))
>
> which doesn't look so bad.  (But then again, without the backslash it's
> less explicit <wink>.)
>

Here's why I personally like this:

>>>> '%s' 'x' % (5)
'5x'
>>> '%s' + 'x' % (5)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: not all arguments converted

Using this form of concatenation instead of the + form allows for the % 
sprintf operator to work correctly.  Joy!

Rich






More information about the Python-list mailing list