[Python-3000] String formating operations in python 3k

Ian Bicking ianb at colorstudy.com
Wed Apr 5 21:34:28 CEST 2006


Tim Hochberg wrote:
> Ian Bicking wrote:
>> Alex Martelli wrote:
>>> As for the % operator, I never liked it -- either a builtin 
>>> function,  or even better a method of string objects, is going to be 
>>> much more  readable (and my preference would be to have it take 
>>> optional  positional arguments, corresponding to {1}, {2}, etc, and 
>>> optional  named arguments, corresponding to {name} &c).
>>
>>
>>
>> Note that if it takes keyword arguments, but doesn't take a single 
>> dictionary-like object (like % and string.Template.substitute do), 
>> then you lose any ability to use clever or interesting dictionary-like 
>> objects for substitution.
>>
> 
> Why doesn't the ** take care of this? To take your example, why doesn't 
> this work?
> 
> string.Template(pattern).substitute(**EscapingWrapper(**EvalingWrapper(**locals())) 
> 
> 
> Is it that you feel that adding ** is too much of a syntactic burden, or 
> am I just missing something?

func(**kw) actually passes a dictionary to func.  EvalingWrapper isn't a 
dictionary, it's just an object with a __getitem__.  ** can't be changed 
in this way, either -- it really has to enumerate all the keys in the 
thing passed to it, and there's no way to enumerate all the keys in a 
wrapper like EvalingWrapper.

Any string substitution only needs __getitem__, so these constraints 
when calling a function with ** do not apply to the actual string 
substition.

-- 
Ian Bicking  /  ianb at colorstudy.com  /  http://blog.ianbicking.org


More information about the Python-3000 mailing list