简体   繁体   中英

Commas and strings in Python 2.7 string.format()

I'm confused by the following Python 2.7 and Python 3.3 behavior in String formatting. This is a nitpicky detail question about how the comma operator interacts with string presentation types.

>>> format(10000, ",d")
'10,000'
>>> format(10000, ",")
'10,000'
>>> format(10000, ",s")
ValueError: Cannot specify ',' with 's'.

>>> "{:,}".format(10000)
'10,000'
>>> "{:,s}".format(10000)
ValueError: Cannot specify ',' with 's'.

What's confusing me is why the , variant works, the one with no explicit string presentation type. The docs say that if you omit the type, it's "The same as s ." And yet here it is acting differently from s .

I'd dismiss this as just a wrinkle / corner case, but this syntax is used as an example in the docs: '{:,}'.format(1234567890) . Are there other "special" behaviors hidden in Python when the string presentation type is omitted? Maybe instead of "same as s" what the code is really doing is inspecting the type of the thing being formatted?

In your example, you aren't interacting with string presentation types; you are interacting with int presentation types. Objects can supply their own formatting behavior by defining a __format__ method. As noted in PEP 3101:

 The new, global built-in function 'format' simply calls this special method, similar to how len() and str() simply call their respective special methods: def format(value, format_spec): return value.__format__(format_spec) Several built-in types, including 'str', 'int', 'float', and 'object' define __format__ methods. This means that if you derive from any of those types, your class will know how to format itself. 

Presentation type s is understandably not implemented by int objects (see the lists of documented presentation types per object type here ). The exception message is somewhat misleading. Without the , , the problem is clearer:

>>> format(10000, "s")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: Unknown format code 's' for object of type 'int'

Refer PEP 378 -- Format Specifier for Thousands Separator

The ',' option is defined as shown above for types 'd', 'e', 'f', 'g', 'E', 'G', '%', 'F' and ''. To allow future extensions, it is undefined for other types: binary, octal, hex, character, etc

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM