Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Is there something similar to ?: operator (C/C++) in Python?

Reply
Thread Tools

Is there something similar to ?: operator (C/C++) in Python?

 
 
Bo Peng
Guest
Posts: n/a
 
      06-19-2005
Hi,

I need to pass a bunch of parameters conditionally. In C/C++, I can do

func(cond1?a:b,cond2?c:d,.....)

In Python, I am using temporary variables like

if cond1:
para1 = a
else:
para1 = b

# ....
# a bunch of such if/else

func(para1, para2,...)

Is there an easier way to do this in Python?

BTW, I do not want to use the following since all of a,b,c,d etc will be
evaluated.

def condValue(cond, val1,val2):
if cond:
return val1
else:
return val2

func(condValue(cond1,a,b), condValue(cond2,c,d),...)

Thanks.
Bo
 
Reply With Quote
 
 
 
 
Peter Hansen
Guest
Posts: n/a
 
      06-19-2005
Bo Peng wrote:
> I need to pass a bunch of parameters conditionally. In C/C++, I can do
> func(cond1?a:b,cond2?c:d,.....)
>
> Is there an easier way to do this in Python?


Please read the FAQ to learn the answer and much other useful
information. In this case, the specific entry follows, but you'd do
well to read all the entries:

http://www.python.org/doc/faq/progra...rnary-operator

-Peter
 
Reply With Quote
 
 
 
 
Hsuan-Yeh Chang
Guest
Posts: n/a
 
      06-19-2005
Bo Peng wrote:

> Hi,
>
> I need to pass a bunch of parameters conditionally. In C/C++, I can do
>
> func(cond1?a:b,cond2?c:d,.....)
>
> In Python, I am using temporary variables like
>
> if cond1:
> para1 = a
> else:
> para1 = b
>
> # ....
> # a bunch of such if/else
>
> func(para1, para2,...)
>
> Is there an easier way to do this in Python?
>
> BTW, I do not want to use the following since all of a,b,c,d etc will be
> evaluated.
>
> def condValue(cond, val1,val2):
> if cond:
> return val1
> else:
> return val2
>
> func(condValue(cond1,a,b), condValue(cond2,c,d),...)
>
> Thanks.
> Bo


take a look at how to use "lambda" in python.

 
Reply With Quote
 
Roy Smith
Guest
Posts: n/a
 
      06-19-2005
Bo Peng <(E-Mail Removed)> wrote:
> I need to pass a bunch of parameters conditionally. In C/C++, I can do
>
> func(cond1?a:b,cond2?c:d,.....)


Python does not have a ternary operator analogous to C's . There are
some ugly hacks you can play with the logical operators to emulate , but
in my opinion, the cure is worse than the disease.

> In Python, I am using temporary variables like
>
> if cond1:
> para1 = a
> else:
> para1 = b
>
> # ....
> # a bunch of such if/else
>
> func(para1, para2,...)


Yeah, that's how I would do it. How many of these things do you have?
 
Reply With Quote
 
Bo Peng
Guest
Posts: n/a
 
      06-19-2005
>>In Python, I am using temporary variables like
>>
>>if cond1:
>> para1 = a
>>else:
>> para1 = b
>>
>># ....
>># a bunch of such if/else
>>
>>func(para1, para2,...)

>
>
> Yeah, that's how I would do it. How many of these things do you have?


I have around 10 of them. Using these if/else, it will take 50 lines for
a function call. It also bothers me to have 10 variables left in the
namespace, in addition to finding 10 meaningful names.

The FAQ provides two solutions, neither of them are elegant. I guess I
will use if/else for reasability purpose.

Thanks.
Bo
 
Reply With Quote
 
Roy Smith
Guest
Posts: n/a
 
      06-19-2005
<d92khl$iop$(E-Mail Removed)>, Bo Peng <(E-Mail Removed)> wrote:
> I have around 10 of them. Using these if/else, it will take 50 lines for
> a function call. It also bothers me to have 10 variables left in the
> namespace, in addition to finding 10 meaningful names.


If you've got 10 different conditional branches in a single function,
that's pretty messy logic no matter what language you write it in or what
syntactic sugar you've got to let you write it compactly. Can you give us
some idea of what it is that you're trying to do? It pretty unusual to see
a requirement like that.

> The FAQ provides two solutions, neither of them are elegant. I guess I
> will use if/else for reasability purpose.


I agree that the and/or hack is ugly, and I generally stay away from it
because I think if/else is easier to read and understand. But if you've
got 10 of them, the compactness (and, as you say, avoiding having to create
10 temp variables) of the and/or probably means the hack is worth doing.
Somthing like:

myComplicatedFunction (cond1 and value1a or value1b,
cond2 and value2a or value2b,
cond3 and value3a or value3b,
cond4 and value4a or value4b,
cond5 and value5a or value5b,
cond6 and value6a or value6b,
cond7 and value7a or value7b,
cond8 and value8a or value8b,
cond9 and value9a or value9b,
cond10 and value10a or value10b)

is kind of ugly, but at least you only have to understand what's going on
with the and/or ONCE, and then you can apply that understanding to the 10
repetitions of the construct. Having 50 lines of if/else makes it much
harder to get your head around what's going on.
 
Reply With Quote
 
gene tani
Guest
Posts: n/a
 
      06-19-2005
If your test variable has specific values to branch on, the standard
way is to have those values be keys in a dictionary, and do:

branched_func_obj = dict_of_values.get(testvar)

And the lambda hack is here, courtesy of Peter Norvig

http://www.norvig.com/python-iaq.html

 
Reply With Quote
 
Bo Peng
Guest
Posts: n/a
 
      06-19-2005
Roy Smith wrote:

> Can you give us some idea of what it is that you're trying to do? It pretty unusual to see
> a requirement like that.


def func(type_of_obj1, type_of_obj2, .....):
callfunc( [
type_of_obj1 and obj1a() or obj1b(),
type_of_obj2 and obj2a() or obj2b(),
....
])

callfunc can take arbitrary number of objects whose types are determined
by type_of_obj1 etc. I was using a bunch of if/else to create objects
and pass them to callfunc.

Since type_of_obj1 etc are usually binary and obj1a() etc will never be
false, the and/or solution does not look so bad in this case.

Thanks.
Bo
 
Reply With Quote
 
Roy Smith
Guest
Posts: n/a
 
      06-19-2005
In article <d9461f$olg$(E-Mail Removed)>, Bo Peng <(E-Mail Removed)> wrote:

> Roy Smith wrote:
>
> > Can you give us some idea of what it is that you're trying to do? It
> > pretty unusual to see
> > a requirement like that.

>
> def func(type_of_obj1, type_of_obj2, .....):
> callfunc( [
> type_of_obj1 and obj1a() or obj1b(),
> type_of_obj2 and obj2a() or obj2b(),
> ....
> ])


I'm still not sure what the big picture is, but I suspect if you're
switching on object types, you're doing something wrong, or at least
unpythonic.
 
Reply With Quote
 
George Sakkis
Guest
Posts: n/a
 
      06-19-2005
"Bo Peng" wrote:

> Roy Smith wrote:
>
> > Can you give us some idea of what it is that you're trying to do? It pretty unusual to see
> > a requirement like that.

>
> def func(type_of_obj1, type_of_obj2, .....):
> callfunc( [
> type_of_obj1 and obj1a() or obj1b(),
> type_of_obj2 and obj2a() or obj2b(),
> ....
> ])
>
> callfunc can take arbitrary number of objects whose types are determined
> by type_of_obj1 etc.


Unless the obj_i_a and obj_i_b are totally unrelated (very unlikely), I
think this might/should be refactored so that the decision between each
obj_i_a and obj_i_b is done in a separate obj_i_factory callable. If
you provide an example or two of specific triples (type_of_obj_i,
obj_i_a, obj_i_b), we'll have a better idea of whether factories are an
overkill in this case or not.

George

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is there something similar to list comprehension in dict? Peng Yu Python 8 11-20-2009 08:37 PM
Re: Is there something similar to list comprehension in dict? Patrick Sabin Python 1 11-20-2009 09:29 AM
Any similar Webcam broadcasting site similar to youtube Chaudhry Nijjhar Computer Support 0 02-19-2008 11:48 PM
Smart Pointers: Is there something similar to smart pointers in C? MotoK C Programming 59 09-15-2006 07:03 PM
Is there a goto statement (or something similar)? Mike42 Java 21 11-14-2005 12:22 PM



Advertisments