Eric V. Smith <e...@trueblade.com> 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. _pydecimal.py 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 _pydecimal.py 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 <rep...@bugs.python.org>
<https://bugs.python.org/issue32820>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to