Short answer: No, using .format(**vars(object)) not possible, since properties do not use __dict__ from the vars documentation vars :
vars(...)
vars([object]) → dictionary
- Without arguments equivalent to
locals() . - With an argument equivalent to
object.__dict__ .
However, you can achieve what you want using various format specifiers, for example, attribute search:
In [2]: '{.bar}'.format(Foo()) Out[2]: 'bar here'
Please note that you just need to add a lead . (dot) to the names, and you get exactly what you want.
Note: instead of .format(**vars(object)) you should use the format_map method:
In [6]: '{baz}'.format_map(vars(Foo())) Out[6]: 'baz here'
Calling format_map with the dict argument is equivalent to calling format using ** notation, but it is more efficient because it does not need to unpack it before calling the function.
source share