[Python-ideas] Outside the box string formatting idea
Ron Adam
ron3200 at gmail.com
Mon Aug 10 04:31:49 CEST 2015
On 08/09/2015 09:11 PM, Rob Cliffe wrote:
>> Comma's could be used to separate things, but it's not that much of a
>> stretch to go from ....
>>
>> 'a' 'b' --> 'ab'
>>
>> to
>> a = 'a'
>> b = 'b'
>> (% a b) --> 'ab'
>>
>> But we could have...
>>
>> (% a, b) If that seems more pythonic.
>>
> How does this gain over
>
> def f(*args): return ''.join(args)
> a='a'
> b='b'
> f(a, b)
To make that work in the same way you would also need to add a way to
handle string conversion of expressions and formatting.
The main gain is the removal of some of the syntax elements and repetive
method or function calls that would be required in more complex situations.
The point was to boil it down to the minimum that could be done and still
not move the expression into the string.
Lets look at this example...
(% 'My name is ' name ', my age next year is ' (age+1)
', my anniversary is ' {anniversary ':%A, %B %d, %Y'} '.')
Using functions and method calls it might become...
>>> f = lambda *args: ''.join(args)
>>> _ = format
>>> import datetime
>>> name = 'Fred'
>>> age = 50
>>> anniversary = datetime.date(1991, 10, 12)
>>> f('My name is ', _(name), ', my age next year is ', _(age+1),
... ', my anniversary is ', _(anniversary, ':%A, %B %d, %Y'), '.')
'My name is Fred, my age next year is 51, my anniversary is :Saturday,
October 12, 1991.'
That isn't that much different and works today. A special format
expression would remove some of the syntax elements and make it a
standardised and cleaner looking solution.
Because it requires the extra steps to define and rename the join and
format functions to something shorter, it's not a standardised solution.
Having it look the same in many programs is valuable. Also Nicks
improvements of combining translation with it might be doable as well.
Even with commas, it's still may be enough. Than again if everyone like
the expressions in strings, it doesn't matter.
Cheers,
Ron
More information about the Python-ideas
mailing list