473,221 Members | 1,749 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,221 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 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.

Similar topics

8
by: Burgess Meredith | last post by:
I suffered through the interminal drivel, that the Dynamic Dummies, freeagent-Man, and the Boy Blunder had to say, > On Thu, 09 Oct 2003 11:57:07 +1000, Chris wrote: > >> On Thu, 09 Oct 2003...
7
by: vee_kay | last post by:
Is there any simpler forum for C++. I feel this forum is advanced for a begineer
2
by: Alessandro Bottoni | last post by:
I just tried to use the python standard logging module in a small program and I found myself lost among all those features, all those configuration options and so on. Is there any better/simpler...
2
by: Greg Strong | last post by:
Hello All, Is there a simpler way to count text boxes with data in them when the 4 text boxes are in the header of a form? I've written the code below and it works. ,----- | Private Sub...
2
by: meh | last post by:
I'm not sure if I have the right concept going. I am making a custom TabPage and executing it from a button click event. It works fine I'm just thinking I might be missing the finer points of the...
1
by: Roy Gourgi | last post by:
Hi, Here is a much simpler version of my time scheduling program. As you can see in the GV (Global Variable class) I have 2 arrays gaSumWorkDays (already initialized with the values) and ...
9
by: seberino | last post by:
Is there any advantage to a language having a nice mathematically compact grammar like LISP does? (or at least used to?) Many have admired the mathematically simple grammar of LISP in which much...
36
by: phil-news-nospam | last post by:
Here is a simpler (no drop shadows) example of the padding bug I see: http://phil.ipal.org/usenet/ciwas/2006-05-08/buttons-1.html So far I find nothing in the CSS2 document that says I should...
6
by: mcse jung | last post by:
Here is asample program that writes a program and then executes it. Do you knowof a much simpler way of writing a program that writes a program? """...
1
by: Serman D. | last post by:
Background: The Payment Card Industry (PCI) Data Security Standard (PCI DSS) is a standard for financial institutions. It requires sensitive information, such as credit card numbers, to be...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.