473,221 Members | 1,749 Online

# Any simpler way to do this

I have a function that looks like this:

def Chooser(color):

if color == "RED":
x = term.RED
elif color == "BLUE":
x = term.BLUE
elif color == "GREEN":
x = term.GREEN
elif color == "YELLOW":
x = term.YELLOW
elif color == "CYAN":
x = term.CYAN
elif color == "MAGENTA":
x = term.MAGENTA
return x
Wouldn there been easier if I could just skip all the "*if's" and just
"return term.color", however this gives a syntax error, are there any
clever way to do this ?
--
Lars Johansen <la**@larsjohansen.org>

Dec 7 '07 #1
4 1007
On Dec 7, 9:37 am, Lars Johansen <l...@larsjohansen.orgwrote:
I have a function that looks like this:

def Chooser(color):

if color == "RED":
x = term.RED
elif color == "BLUE":
x = term.BLUE
elif color == "GREEN":
x = term.GREEN
elif color == "YELLOW":
x = term.YELLOW
elif color == "CYAN":
x = term.CYAN
elif color == "MAGENTA":
x = term.MAGENTA
return x

Wouldn there been easier if I could just skip all the "*if's" and just
"return term.color", however this gives a syntax error, are there any
clever way to do this ?
--
Lars Johansen <l...@larsjohansen.org>
"return getattr(term, color)" should do the trick.
Dec 7 '07 #2
On Dec 7, 10:37 am, Lars Johansen <l...@larsjohansen.orgwrote:
I have a function that looks like this:

def Chooser(color):

if color == "RED":
x = term.RED
elif color == "BLUE":
x = term.BLUE
elif color == "GREEN":
x = term.GREEN
elif color == "YELLOW":
x = term.YELLOW
elif color == "CYAN":
x = term.CYAN
elif color == "MAGENTA":
x = term.MAGENTA
return x

Wouldn there been easier if I could just skip all the "*if's" and just
"return term.color", however this gives a syntax error, are there any
clever way to do this ?
--
Lars Johansen <l...@larsjohansen.org>
def Chooser(color):
return getattr(term, color)

You could also do:

def Chooser(color):
return getattr(term, colour, 'Default response if colour doesn't
exist')
Dec 7 '07 #3
Lars Johansen a écrit :
I have a function that looks like this:

def Chooser(color):
<mode="pedant">
Bad naming (noun instead of a verb) and not conformant to PEP 08
(function names should be all_lower)
</mode>
if color == "RED":
x = term.RED
elif color == "BLUE":
x = term.BLUE
elif color == "GREEN":
x = term.GREEN
elif color == "YELLOW":
x = term.YELLOW
elif color == "CYAN":
x = term.CYAN
elif color == "MAGENTA":
x = term.MAGENTA
return x

What do you think will happen if I call it like this:
Chooser(42)

(if you answered 'will raise a NameError', then you're correct).
>
Wouldn there been easier if I could just skip all the "*if's" and just
"return term.color", however this gives a syntax error, are there any
clever way to do this ?
getattr(obj, name) is your friend.
Dec 7 '07 #4
On Fri, 07 Dec 2007 09:37:22 +0100, Lars Johansen wrote:
I have a function that looks like this:

def Chooser(color):

if color == "RED":
x = term.RED
[snip]
Wouldn there been easier if I could just skip all the "*if's" and just
"return term.color", however this gives a syntax error, are there any
clever way to do this ?
Others have answered the immediate question, but I'm kind of surprised
that nobody seems to have noticed that Lars is reporting a syntax error
instead of an attribute error.

I suspect that Lars is actually misreporting the error he gets. That's
probably not a good habit to get into :)
--
Steven
Dec 7 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.