Yet another Python textbook

Mark Lawrence breamoreboy at yahoo.co.uk
Wed Nov 21 18:32:34 EST 2012


On 21/11/2012 23:21, Joshua Landau wrote:
> On 21 November 2012 22:17, Chris Angelico <rosuav at gmail.com> wrote:
>
>> On Thu, Nov 22, 2012 at 4:03 AM, Colin J. Williams <cjw at ncf.ca> wrote:
>>> On 20/11/2012 4:00 PM, Chris Angelico wrote:
>>>> To the OP: jmf has an unnatural hatred of Python 3.3 and PEP 393
>>>> strings. Take no notice; the rest of the world sees this as a huge
>>>> advantage. Python is now in a VERY small group of languages (I'm aware
>>>> of just one other) that have absolutely proper Unicode handling *and*
>>>> efficient string handling.
>>>>
>>>> ChrisA
>>>>
>>> It's interesting to see that someone else finds the format function to
>> be a
>>> pain.  Perhaps the problem lies with the documentation.
>>
>> Hang on, what? I'm not sure where the format function comes in. I was
>> referring to the underlying representation.
>>
>> That said, though, I'm just glad that %-formatting is staying. It's an
>> extremely expressive string formatting method, and exists in many
>> languages (thanks to C's heritage). Pike's version is insanely
>> powerful, Python's is more like C's, but all three are compact and
>> convenient.
>>
>> str.format(), on the other hand, is flexible. It strikes me as rather
>> more complicated than a string formatting function needs to be, but
>> that may be a cost of its flexibility.
>>
>
> Since we've decided to derail the conversation...
>
> "{}".format() is a blessing an "" % () should go. "%" has no relevance to
> strings, is hard to "get" and has an appalling* syntax. Having two syntaxes
> just makes things less obvious, and the right choice rarer.
>
> str.format is also really easy. I don't understand what makes you disagree.
>
> Easy vs easier:
>
>>>> "%s %s %s" % (1, 2, 3)
> '1 2 3'
>
>>>> "{} {} {}".format(1, 2, 3)
> '1 2 3'
>
> Easy vs easier:
>
>>>> "You have %(spam)s spam and %(eggs)s eggs!" % {"spam": 43, "eggs": 120}
> 'You have 43 spam and 120 eggs!'
>
>>>> "You have {spam} spam and {eggs} eggs!".format(spam=43, eggs=120)
> <OR>
>>>> "You have {spam} spam and {eggs} eggs!".format(**{"spam": 43, "eggs":
> 120})
> 'You have 43 spam and 120 eggs!'
>
> Eh...? vs easy:
>
>>>> "Thing %s has state %+o!" % ("#432", 14)
> 'Thing #432 has state +16!
>
>>>> "Thing {} has state {:+o}!".format("#432", 14)
> 'Thing #432 has state +16!'
>
> *Additionally*, a = str.format is much *better* than a = str.__mod__.
>
> I have a piece of code like this:
> "{fuscia}{{category__name}}/{reset}{{name}} {green}{{version}}{reset}:\n
>   {{description}}"
>
> Which *would* have looked like this:
> "%(fuscia)s%%(category__name)s/%(reset)s%%(name)s
> %(green)s%%(version)s%(reset)s:\n    %%(description)s"
>
> Which would have parsed to something like:
> 'FUSCIA{category__name}/RESET{name} GREEN{version}RESET:\n    {description}'
> and
> 'FUSCIA%(category__name)s/RESET%(name)s GREEN%(version)sRESET:\n
>   %(description)s'
>
> Can you seriously say you don't mind the "%(name)s"s in this?
>
> * "A {} is in the {}" vs "A %s is in the %s"?
>
>
>

C %f style formatting is never going to go so live with it.  I know as I 
asked maybe two years ago.  On Python-dev.  I think.

-- 
Cheers.

Mark Lawrence.




More information about the Python-list mailing list