[Python-3000] Changing the % format return type Re: features i'd like [Python 3000?] ... #4: interpolated strings ala perl

Jan Grant jan.grant at bristol.ac.uk
Wed Dec 6 12:44:14 CET 2006


Ka-Ping Yee's suggestion of overriding the interpolation builtin (or 
let's say, the "%" operator used for formatting) means there might be 
another way to do this using GFs: let's say that the following 
illustrative snippet is still desirable -

On Wed, 6 Dec 2006, Jan Grant wrote:

> def foo(d: sqlDriver):
>   s = "select template goes here" % (param1, param2)
>   r = d.query(s)	# do the Right Thing

Suppose for a moment that % used like this returns, not a string, but an 
object that encapsulates the format and the parameter set supplied.

Then the sqlDriver implementation could take advantage of Phillip's GF 
mechanisms thus:

class driver 	# which implements sqlDriver
  def query(self, s: str):
    ... code here to perform a query based on a stock string ...

  def query(self, pf: parameterised_format):
    ... code here to extract the format string and quote the
    ... parameters appropriately within it.

The _cost_ of this mechanism is that all library functions potentially 
have to become aware of the parameterised_format type. Alternatively 
(and although explicit is better than implicit), a GF framework might 
look to coerce a parameterised_format into a str using something like 
adaption. Or "parameterised_format" could be a _subtype_ of "str", which 
behaves exactly like the str type if it is naively used in contexts 
which demand a plain string. In the latter case (if achievable, I have 
not examined this closely) a %-formatted object would "degrade" to the 
simple string case for most current uses.

jan


-- 
jan grant, ISYS, University of Bristol. http://www.bris.ac.uk/
Tel +44 (0)117 3317661   http://ioctl.org/jan/
perl -e 's?ck?t??print:perl==pants if $_="Just Another Perl Hacker\n"'


More information about the Python-3000 mailing list