semi-concatenated strings

Grant Griffin Grant_member at newsguy.com
Fri May 31 11:21:11 EDT 2002


In article <mailman.1022791249.9251.python-list at python.org>, Skip says...
>
>
>    Grant> I discovered today that strings can sometimes be concatenated
>    Grant> without using a "+":
>
>Actually, string literals can always be concatenated without adding them.
>
>    Grant> I discovered this, of course, while making a mistake like this:
>
>    >>> a = ['zero', 'one'
>    ...       'two', 'three']
>    >>> a
>    ['zero', 'onetwo', 'three']
>
>Yup, that's just how it's supposed to work.  It makes it easier to compose
>long strings.  For example, I have code like this that queries a database:
>
>    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,))
>
>At compile time all those strings are concatenated into one long string.
>The select statement remains readable for me, but is represented as a single
>string constant in the generated bytecode.

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>.)

explict-is-better-than-implicit-for-a-reason-ly y'rs,

=g2

_________________________________________________________________________

Grant R. Griffin                                           g2 at dspguru.com
Publisher of dspGuru                               http://www.dspguru.com
Iowegian International Corporation                http://www.iowegian.com




More information about the Python-list mailing list