4

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?

2 Answers 2

3

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'
Sign up to request clarification or add additional context in comments.

1 Comment

Ah, the scales have fallen from my eyes! Every type in Python (potentially) defines its own presentation type language via __format__(self, format_spec). So potentially 'd' or '' could mean anything at all for some particular type, although there's convention. The bit in the docs about no type being "same as s" is for the string presentation types. Just a bit further down the docs, for integer presentation types, it says None is "same as d". So now it all makes sense.
0

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

1 Comment

Ah, I'd missed the inclusion of '' in that description; that'd be the empty string, no type? If so at least someone intended the behavior I'm seeing, but I'm still curious what's going on and why it's inconsistent with the docs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.