Any simpler way to do this
Chris
cwitts at gmail.com
Fri Dec 7 04:07:12 EST 2007
More information about the Python-list mailing list
Fri Dec 7 04:07:12 EST 2007
- Previous message (by thread): Any simpler way to do this
- Next message (by thread): Any simpler way to do this
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On Dec 7, 10:37 am, Lars Johansen <l... at larsjohansen.org> wrote: > 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... at 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')
- Previous message (by thread): Any simpler way to do this
- Next message (by thread): Any simpler way to do this
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list