471,317 Members | 1,339 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

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 952
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Burgess Meredith | last post: by
7 posts views Thread by vee_kay | last post: by
2 posts views Thread by Alessandro Bottoni | last post: by
2 posts views Thread by meh | last post: by
36 posts views Thread by phil-news-nospam | last post: by
reply views Thread by rosydwin | last post: by

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.