Issue 9139: Add examples for str.format() (original) (raw)
Created on 2010-07-02 02:43 by ezio.melotti, last changed 2022-04-11 14:57 by admin. This issue is now closed.
Messages (11)
Author: Ezio Melotti (ezio.melotti) *
Date: 2010-07-02 02:43
The attached patch adds a section with examples about str.format() after http://docs.python.org/library/string.html#format-specification-mini-language . The patch needs some small improvements:
- the examples in the previous sections could be removed and/or a link to the new section could be added;
- the last example could be improved;
- another example using custom formats (e.g. for datetime) should be added;
I'd like to have this in 2.7 (Benjamin said it's OK), please review.
Author: Éric Araujo (eric.araujo) *
Date: 2010-07-02 10:34
The examples seem good to me after a quick reading. There’s not too much but still enough.
I find the phrasing of the versionchanged not easily understandable: “The position of the arguments can now be omitted.” Clearer and probably too verbose: “When using numeric placeholders that follow the order of the positional arguments, you can omit the numbers: '{} {!s}' is equivalent to '{0} {1!s}'”
Author: Eric V. Smith (eric.smith) *
Date: 2010-07-02 14:23
Thanks for taking the lead on this.
In this sentence: +The new format syntax also supports new and different options, showed in the +follow examples. "showed" should be "shown".
I like to use an example showing attribute access on named types. Barry Warsaw suggested using "self", which is a great tip. It's hard to come up with a complete example, but something that ends up like: "x is {self.x}, y is {self.y}".format(self=self)
I think it would be a good idea to show comma formatting (even with its limitations), since there's not another easy way to do that in Python:
'{:,}'.format(1234567890) '1,234,567,890'
For datetime, something like:
import datetime d = datetime.datetime(2010, 7, 4, 12, 15, 58) '{:%Y-%m-%d %H:%M:%S}'.format(d) '2010-07-04 12:15:58'
Author: Eric V. Smith (eric.smith) *
Date: 2010-07-02 14:24
Instead of "named types", I meant "named arguments", of course.
Author: Ezio Melotti (ezio.melotti) *
Date: 2010-07-02 18:28
Here is a new patch. I fixed the typos, removed the examples from the first section and added a link to the examples section. I also added the examples suggested by Eric plus an example about {:%}, and followed the suggestion of Éric and clarified the versionchanged message.
Author: Eric V. Smith (eric.smith) *
Date: 2010-07-02 20:47
This looks like a definite improvement to me.
When you merge to py3k, don't forget to change the versionchanged tag to 3.1.
Author: Éric Araujo (eric.araujo) *
Date: 2010-07-02 21:33
I’m not sure it’s good style to say “now” twice (in the versionchanged block).
I almost feel bad pointing that there is one line with bad whitespace:
for align,text in ... (Space after comma)
Otherwise great patch :)
Author: Eric V. Smith (eric.smith) *
Date: 2010-07-02 21:41
I think I'd just delete both instances of "now". And now that I read it more critically, "position" should be "positional". How's this rewording:
.. versionchanged:: 2.7
The positional argument specifiers can be omitted, so '{} {}'
is
equivalent to '{0} {1}'
.
Author: Ezio Melotti (ezio.melotti) *
Date: 2010-07-02 21:50
Here is another patch. I also added another example that uses the fillchar.
Author: Eric V. Smith (eric.smith) *
Date: 2010-07-02 22:23
Looks good.
Author: Ezio Melotti (ezio.melotti) *
Date: 2010-07-02 23:24
Committed in r82457 and r82459 (trunk), r82460 (release26-maint), r82462 (py3k), r82463 (release31-maint). Thanks for the reviews!
History
Date
User
Action
Args
2022-04-11 14:57:03
admin
set
github: 53385
2010-07-02 23:24:31
ezio.melotti
set
status: open -> closed
resolution: accepted -> fixed
messages: +
stage: patch review -> resolved
2010-07-02 22:23:48
eric.smith
set
messages: +
2010-07-02 21:50:59
ezio.melotti
set
files: + issue9139v3.diff
messages: +
2010-07-02 21:41:17
eric.smith
set
messages: +
2010-07-02 21:33:23
eric.araujo
set
resolution: accepted
messages: +
2010-07-02 20:47:06
eric.smith
set
messages: +
2010-07-02 18:32:07
mark.dickinson
set
nosy: + mark.dickinson
2010-07-02 18:28:37
ezio.melotti
set
files: + issue9139v2.diff
messages: +
2010-07-02 14:24:47
eric.smith
set
messages: +
2010-07-02 14:23:41
eric.smith
set
messages: +
2010-07-02 10:34:23
eric.araujo
set
nosy: + eric.araujo
messages: +
2010-07-02 02:43:07
ezio.melotti
create