[Python-Dev] Can we please have a better dict interpolation syntax?

François Pinard pinard at iro.umontreal.ca
Tue Jul 13 16:08:07 CEST 2004


Hi again.  Getting back to much older messages about `$'-interpolation. :-)

[Barry Warsaw]
> [François Pinardi]:
> > [Guido van Rossum]
> > > > > Wouldn't this be even better?
> > > > >     "create index ${table}_lid1_idx on $table($lid1)" % params

> > "Better" because it uses `$' instead of `%'? It is really a matter
> > of taste and aesthetics, more than being "better" on technical
> > grounds.  Technically, the multiplication of aspects and paradigms
> > goes against some unencumberance and simplicity, which made Python
> > attractive to start with.  We would loose something probably not
> > worth the gain.

> Better because the trailing type specifier on %-strings is extremely
> error prone (#1 cause of bugs for Mailman translators is/was leaving
> off the trailing 's').

Such errors are usually caught much more solidly, for example, right
in `msgfmt'.  At least for C, and most likely for Python as well,
it is kind of catastrophic for an application that its translation
file has errors regarding formats.  If a bad PO file crashes an
application, maintainers and users will get mad at internationalisation.
Forgetting a trailing `s' is one error, misspelling a variable name is
another: both should absolutely be caught before the release of an
internationalised application.  Using $-strings instead of %-strings is
far from adequately addressing the real problem.  Translators, despite
all their good will, cannot be trusted to never make errors, and they
usually use `msgfmt' to validate their own PO files before transmission
(or indirectly rely on robots for doing that validation for them).
Whatever they use $-strings or %-strings, validation is necessary.

> Better because the rules for $-strings are simple and easy to explain.

I just read PEP 292 again, and a mapping is necessarily provided, this
evacuates a lot of questions about how variables would be accessed out
of local and global scopes -- this simplifies things a lot.  The PEP
does not say what the behaviour of the substitution when an identifier
is not part of the mapping, and this is an important issue.

At one place, the PEP says that `dstring' could be sub-classed to get
different behaviour, like allowing dots for attribute access, but
elsewhere, it also implies that within "${identifier}", "identifier"
has to be an identifier, that is, no dots.  And if "identifier" could
contain special characters like dots or brackets, it does not say if
brackets may be nested nor if they have to balance (like they apparently
and nicely do with `%' interpolation).

It does not seem all that simple and easy for me.  Granted it could have
been much more difficult.

> And yes, better because it uses $ instead of %; it just seems that
> more people grok that $foo is a placeholder.

Yet, users of `$' in other languages or scripts do not have to
explicitly provide a mapping, so the similarity stays a bit superficial.
But if this makes a few more users happy, and being in a library, stays
away from the Python language, `$-strings' may indeed serve a purpose.

-- 
François Pinard   http://www.iro.umontreal.ca/~pinard


More information about the Python-Dev mailing list