Eric V. Smith <> added the comment:

If you don't recognize the format string, you want to fall back to:

return super().__format__(fmt)

Since that will call object.__format__, it will generate an error if fmt is not 
the empty string, which is what it does currently for IP addresses.

As far as parsing the format specifier, you're on your own. has a 
version that parses something very similar (or maybe identical) to the 
mini-language used by float, int, str, etc. But I'm not sure you need to go 
that far. Since you can define any format spec you'd like, you're not 
constrained to the mini-language used by the built-in types (see datetime for 
an example, which just calls strftime). I think the approach of 
using a regex isn't bad, but you might want to delay loading re until 
__format__ is called.

Before you go too far down this path, I think this should probably be taken to 
python-ideas to hash out what the format spec for IP addresses would look like, 
or even if this is a good idea at all. I'm generally supportive of handling it 
through __format__().


Python tracker <>
Python-bugs-list mailing list

Reply via email to