Is there something similar to ?: operator (C/C++) in Python?
Ron Adam
rrr at ronadam.com
Sun Jun 19 14:15:56 EDT 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. 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
Are you matching the order to the obj_type?
objlist = [ (type_obj1, obj1a, obj2b),
(typ_obj2, obj2a, obj2b),
etc...]
objs = [type_of_obj1, type_of_obj2, etc...]
for n in range(len(objs)):
if objs[n] == objlist[n][0]:
objlist[n][1]()
else:
objlist[n][2]()
What significance does the order have?
You might be able to use a dictionary of tuples.
call_obj = {(type_obj1,0):obj1a,
(type_obj1,0):obj1b,
(type_boj2,1):obj2a,
(type_obj2,1):obj2b,
etc... }
call_obj[(type_of_obj,order)]()
Regards, Ron
More information about the Python-list
mailing list