Any simpler way to do this

L

Lars Johansen

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 ?
 
V

Virgil Dupras

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 ?

"return getattr(term, color)" should do the trick.
 
C

Chris

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 ?

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')
 
B

Bruno Desthuilliers

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)
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.
 
S

Steven D'Aprano

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 :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top