something like "if" that returns a value?
holger krekel
pyth at devel.trillke.net
Mon Nov 11 16:54:37 EST 2002
Paul Rudin wrote:
> >>>>> "holger" == holger krekel <pyth at devel.trillke.net> writes:
>
>
> holger> if you are doing this often of course you could write a
> holger> helper... but
>
> But what kind of helper? Writing a function doesn't get round the
> unnecessary evaluation problem. (Arguably it also doesn't entirely help
> with the obfuscation problem either.)
yip, you are basically right although there are some tricks.
> >> 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..)
>
> holger> def foo(*args): return func1(*args) + (func2,func3)[not
> holger> some_test](*args)
>
> Yah - I didn't mean to imply that the arity and order of the arguments
> was necessarily always the same.
It appeared to me that 'some_test' was a switch for dispatching
to either of two functions taking the same arguments.
btw, if you have a series of tests and even more functions then
python syntax and idioms scale quite well (IMO, of course).
I am sure that the more concrete your code snippet is the better
i can come up with a concise syntactic/semantic minimalization.
but it's of course ok, too, to stop if you've had enough :-)
cheers,
holger
More information about the Python-list
mailing list