[Python-Dev] PEP 292, Simpler String Substitutions

Barry A. Warsaw barry@zope.com
Wed, 19 Jun 2002 08:29:39 -0400


>>>>> "DB" == Duncan Booth <duncan@rcp.co.uk> writes:

    DB> What I really don't understand is why there is such pressure
    DB> to get an alternative interpolation added as methods to str &
    DB> unicode rather than just adding an interpolation module to the
    DB> library?  e.g.

Because I don't think there's all that much useful variation, open
issues in this PEP notwithstanding.  A module seems pretty heavy for
such a simple addition.  It might obviate the need for a PEP
though. :)

    | from interpolation import sub
    | def birth(self, name):
    |     country = self.countryOfOrigin['name']
    |     return sub('${name} was born in ${country}', vars())

    DB> I added in the explicit vars() parameter because the idea of a
    DB> possibly unknown template string picking up arbitrary
    DB> variables is, IMHO, a BAD idea.

Only if the template string comes from an untrusted source.  If it's
in your code, there should be no problem, and if there is, it's a
programming bug.

vars() doesn't cut it as mentioned in a previous reply.

-Barry