I am writing a Python program that generates a C++ program. There are a whole bunch of instances of code like this:
class TestBlock(object):
def __init__(self, mod, name, casetype, generator):
self.mod = mod
self.name = name
self.casetype = casetype
self.generator = generator
def fullname(self):
return "{mod}.{name}".format(**self.__dict__)
def write_cases(self, outf):
outf.write("const {casetype} {mod}_{name}[] = {{\n"
.format(**self.__dict__))
for case in self.generator():
outf.write(" { " + case + " },\n")
outf.write("};\n\n")
The "text {subst}".format(**self.__dict__) construction is the only way I have found to make the instance variables of self available as named substitutions, and it’s ugly and I do not like it. I would like to be able to write "text {subst}".format(self); is this possible, and if so, how? I’m aware that "text {0.subst}".format(self) will work out of the box and "text {subst}".format(**self) can be made to work with a few special methods, but both of those still have extra gunk.
You could wrap the functionality in a function:
Then: