Oh, I didn't even know about `format_map`! Or `__format__` for that matter. 
This makes everything much easier. For my purposes, I don't even need to use 
`__format__` since I am not using format strings.

In a show of appreciation, here is a URL to a GIF of James Corden bowing:
https://media.giphy.com/media/l2R0eYcNq9rJUsVAA/giphy.gif

```
class Options(UserDict):
    def __getitem__(self, key):
        if key in self:
            return super().__getitem__(key)
        key, *default = key.split('?')
        if key in self:
            return super().__getitem__(key)
        return ''.join(default)

options = {'baud': 19200}
result = 'BAUD: {baud?9600}'.format_map(Options(options))
print(result)

options = {}
result = 'BAUD: {baud?9600}'.format_map(Options(options))
print(result)

```
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/HPV3AJ4PUYDGVWAKLQQQL7EEHSRBP7N7/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to