something like "if" that returns a value?
holger krekel
pyth at devel.trillke.net
Mon Nov 11 15:44:19 EST 2002
Paul Rudin wrote:
> >>>>> "holger" == holger krekel <pyth at devel.trillke.net> writes:
>
>
>
> holger> Is there an obvious killer use case? IOW can you
> holger> state a problem where the use of a ternary operator comes in
> holger> really handy? Sorry if this sounds dumb to you but i am interested.
>
> I'm not sure it's that big a deal, but looking back over some of the
> bits and pieces I wrote over the weekend there are a number of code
> fragments that follow this kind of pattern:
let's see.
> def foo(x,y,...):
> acc=func1(x,y....)
> if some_test:
> acc = acc+func2(x,y...)
> else:
> acc = acc+func3(x,y...)
> return acc
if you are doing this often of course you could write a helper... but
> Personally I'd prefer to be able to write something like:
>
> def foo(x,y,....):
> return func1(x,y,..) +
> if some_test:
> func2(x,y...)
> else:
> func3(x,y..)
def foo(*args):
return func1(*args) + (func2,func3)[not some_test](*args)
might do what you want. (*args expresses "all positional arguments")
cheers,
holger
More information about the Python-list
mailing list