I have a function which takes no arguments and returns a string, which I want to be called using a string format. Here it is, and here's how I've tried to use format:
def cabbages():
return 'hello'
In [2]: '{cabbages} world'.format(**locals())
Out[2]: '<function cabbages at 0x101f75578> world'
In [3]: '{cabbages()} world'.format(**locals())
KeyError: 'cabbages()'
So neither of which is quite what I want, i.e. the value of cabbages().
PEP 3101 describes some way in which string.Formatter can be overwritten but it doesn't seem to give many examples. How can I subclass/customise the string Formatter class to do this?
A hacky thing I considered would be to overwrite the __getattr__ method of cabbages, and I really don't want be "considered pathological" (or, at least, *that* pathological).
str.format()withlocals()is generally a really bad idea. Just pass the values you want.