Making Variable Text Output More Pythonic?

Casey casey.mcginty at gmail.com
Thu May 15 16:21:44 EDT 2008


Hi,

I have some classes that print variable outputs depending on their
internal state, like so:

def __str__(self):
    out = []
    if self.opt1: out += ['option 1 is %s' % self.opt1']
    if self.opt2: out += ['option 2 is %s' % self.opt2']
    ....
    return '\n'.join(out)

Is there any way to make this cleaner?



More information about the Python-list mailing list