Reputation: 23
I've been constantly coding in Python using this inefficient style
checkbox = self.request.get(u'checkbox') # get data from a web form
if checkbox == u'yes':
someclass.enabled = True
else:
someclass.enabled = False
how do I shorten this?
Upvotes: 2
Views: 424
Reputation: 123393
As pointed-out in another answer you could use dispatch table to do different things based on a value. However using a dictionary's get()
method rather than \performing a direct lookup would allow you to also easily handle cases where nothing matches. Since the mapping won't be used again it can be temporary and anonymous.
This approach is very generic and can expanded as necessary, but usually requires extra functions to be written. Because of the latter, for very simple cases like your example, one of the other answers would probably require the least effort.
def do_yes(): print 'do_yes'
def do_no(): print 'do_no'
def do_maybe(): print 'do_maybe'
def no_match(): print 'no_match'
{
u'yes': do_yes,
u'no': do_no,
u'maybe': do_maybe,
}.get(self.request.get(u'checkbox'), no_match) ()
Upvotes: -1
Reputation: 4308
It's a little unclear if you used booleans in your example because they were inherent to your problem or because they were a convenient example. If you want to assign to variables more complicated types than booleans, you may also want to check out Python's ternary operator (if you're using version 2.5 or greater):
someclass.int_val = 1 if checkbox == u'yes' else 2
which translates to
if checkbox == u'yes':
someclass.int_val = 1
else
someclass.int_val = 2
For boolean variables, I'd recommend using Yuushi's solution, but for completeness, this is what it would look like:
someclass.enabled = True if checkbox == u'yes' else False
It's about the same amount of typing, but saves some vertical space, which can be useful.
Upvotes: 1
Reputation: 41633
If you ever need more than a boolean value, you should consider using the dispatch pattern:
targets = {
'yes': do_yes,
'no': do_no,
'maybe': do_maybe,
}
targets[self.request.get(u'tricheckbox')]()
# where do_yes, do_no, and do_maybe are the functions to call for each state.
Upvotes: 0
Reputation: 21243
Python eval the statement and return the output to the statement. So you can use the assign variable in right side.
like
variable = eval_statment
so your example will be
someclass.enabled = self.request.get(u'checkbox') == u'yes'
Upvotes: 1
Reputation: 1284
As checkbox == u'yes'
returns a boolean value you can simply assign this result to the variable directly.
someclass.enabled = (checkbox == u'yes')
Upvotes: 2
Reputation: 14952
You can just set the value to the outcome of the statement:
checkbox = self.request.get(u'checkbox') # get data from a web form
someclass.enabled = checkbox == u'yes'
Upvotes: 2
Reputation: 26040
You can do this without an if
statement:
someclass.enabled = (checkbox == u'yes')
Upvotes: 4
Reputation: 12267
Perhaps you could split it into a different function:
def getCheckboxValue(name):
return (self.request.get(name) == u'yes')
Upvotes: 1