471,108 Members | 1,298 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,108 software developers and data experts.

Making Variable Text Output More Pythonic?

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?
Jun 27 '08 #1
4 847
Casey <ca***********@gmail.comwrites:
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?
Maybe.

Have a dictionary of options rather than individual attributes;
options not in the dictionary are not set. E.g.

mask = {
'opt1': 'option 1 is %s',
'opt2': 'option 2 is %s',
...
}

def __str__(self):
return '\n'.join(mask[o] % v for o,v in self.options.iteritems())

--
Arnaud
Jun 27 '08 #2
Arnaud's code wont work if self.opt1 is None, an empty list, an empty
tuple, False, etc, because all these evaluate to false. They wont
print the internal state of these variables. [Just an informational
notice, this may be the behavior you expect]

Secondly, I'm not sure if you know the variable names from before hand
in which case Casey's approach will work, or you need to know them via
introspection. http://www.ibm.com/developerworks/library/l-pyint.html
[Scroll down to attributes].

On May 16, 1:44*am, Arnaud Delobelle <arno...@googlemail.comwrote:
Casey <casey.mcgi...@gmail.comwrites:
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?

Maybe.

Have a dictionary of options rather than individual attributes;
options not in the dictionary are not set. E.g.

mask = {
* * 'opt1': 'option 1 is %s',
* * 'opt2': 'option 2 is %s',
* * ...
* * }

def __str__(self):
* * return '\n'.join(mask[o] % v for o,v in self.options.iteritems())

--
Arnaud
Jun 27 '08 #3
afrobeard <af*******@gmail.comwrites:
Arnaud's code wont work if self.opt1 is None, an empty list, an empty
tuple, False, etc, because all these evaluate to false. They wont
print the internal state of these variables. [Just an informational
notice, this may be the behavior you expect]
??? My suggestion is to get rid of attributes altogether and does not
test any truth values.
Secondly, I'm not sure if you know the variable names from before hand
in which case Casey's approach will work, or you need to know them via
introspection. http://www.ibm.com/developerworks/library/l-pyint.html
[Scroll down to attributes].

On May 16, 1:44*am, Arnaud Delobelle <arno...@googlemail.comwrote:
>Casey <casey.mcgi...@gmail.comwrites:
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?

Maybe.

Have a dictionary of options rather than individual attributes;
options not in the dictionary are not set. E.g.

mask = {
* * 'opt1': 'option 1 is %s',
* * 'opt2': 'option 2 is %s',
* * ...
* * }

def __str__(self):
* * return '\n'.join(mask[o] % v for o,v in self.options.iteritems())

--
Arnaud
Jun 27 '08 #4
I-T
A thousand apologies for my ignorance. I'll try not to get names mixed
up again in the future.

On May 16, 8:42*pm, Arnaud Delobelle <arno...@googlemail.comwrote:
afrobeard <afrobe...@gmail.comwrites:
Arnaud's code wont work if self.opt1 is None, an empty list, an empty
tuple, False, etc, because all these evaluate to false. They wont
print the internal state of these variables. [Just an informational
notice, this may be the behavior you expect]

??? My suggestion is to get rid of attributes altogether and does not
test any truth values.
Secondly, I'm not sure if you know the variable names from before hand
in which case Casey's approach will work, or you need to know them via
introspection.http://www.ibm.com/developerworks/library/l-pyint.html
[Scroll down to attributes].
On May 16, 1:44*am, Arnaud Delobelle <arno...@googlemail.comwrote:
Casey <casey.mcgi...@gmail.comwrites:
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?
Maybe.
Have a dictionary of options rather than individual attributes;
options not in the dictionary are not set. E.g.
mask = {
* * 'opt1': 'option 1 is %s',
* * 'opt2': 'option 2 is %s',
* * ...
* * }
def __str__(self):
* * return '\n'.join(mask[o] % v for o,v in self.options.iteritems())
--
Arnaud
Jun 27 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.