[Python-3000] features i'd like [Python 3000?] ... #4: interpolated strings ala perl

Jan Grant jan.grant at bristol.ac.uk
Wed Dec 6 11:24:20 CET 2006


On Wed, 6 Dec 2006, Ka-Ping Yee wrote:

[well-articulated point made]

Thank-you: I agree with pretty much everything you say. It's an 
interesting question to ask, "can one construct a convenient 
interpolation mechanism that uses the appropriate application quoting 
mechanisms where necessary?"

> If i understand you correctly, your argument is:
> 
>     Many people use simple string interpolation when they should
>     be using an application-specific quoting mechanism.
> 
>     Therefore string interpolation should be inconvenient.

Actually, "unavailable", but yup :-)

>     Therefore application-specific quoting mechanisms should
>     be more convenient to create and use.

Agreed, if this is possible whilst remaining "Pythonic".

> Where does this design constraint take us?
> 
> One possibility it suggests is that the interpolation function could
> be generic, allowing the formatting template to be of a custom type
> that supports application-specific quoting.  If we go this route,
> that would argue in favour of solutions like a method on the
> template object or an infix operator, and would reject solutions
> like a built-in interpolated string type.

That's pretty much the conclusion I'd come to...

> Another possibility is that the interpolation function, if a built-in,
> could be overridable.

I think the question here is, is there a safe, convenient way that 
Python can determine what to do (or be told what to do) when it sees:

	s = "some string here with {variable} in it"

If you have a good approach to the second possibility then I'd be very 
interested.

Cheers,
jan

PS. IF generic functions (operators) could be sensitive to return types 
and IF python6k supported a complex type inference mechanism (possibly 
including automatic coercion), then one might be able to do something 
like:

def foo(d: sqlDriver):
  s = "select template goes here" % (param1, param2)
  r = d.query(s)

where sqlDriver has a method

def query(self, s: some_type_that_signals_sql_interpolation)

and "%" is overloadable on the basis that the return type is determined 
to be compatible with "some_type_that_signals_sql_interpolation". Those 
are some mighty big "IF"s though, and you could still concoct cases 
where things would break :-)

-- 
jan grant, ISYS, University of Bristol. http://www.bris.ac.uk/
Tel +44 (0)117 3317661   http://ioctl.org/jan/
The Java disclaimer: values of 'anywhere' may vary between regions.


More information about the Python-3000 mailing list