[Zope3-Users] Re: Fwd: Re: test() in TALES in Zope3

Michael Dudzik mdudzikml at gmail.com
Wed Feb 1 14:38:15 EST 2006


On Wed, 01 Feb 2006 20:26:24 +0100, "Florent Guillaume" <fg at nuxeo.com>
said:
> Michael Dudzik wrote:
> > Sure, but what about when you don't know the boolean value ahead of
> > time, like:
> > 
> >    C and f(x) or g(x)
> > 
> >    On the other hand, (g(x), f(x))[C] works every time
> > 
> > To be fair, both g(x) and f(x) are evaluated while
> > 
> >    f(x) if C else g(x)  # python 2.5, only evaluates the appropriate
> >    function.
> > 
> > Still, I mostly spoke up because if one starts to use an idiom like:
> > 
> >     python:C and 'somestring' or 'anotherstring'
> > 
> > you have to realize that
> > 
> >     python:C and '' or 'anotherstring'
> > 
> > will fail.  An idiom that breaks so easily doesn't seem so useful.
> 
> In 90% of cases you'll have a fixed value for the true case or the false 
> case, and you'll know that one cannot evaluate to false, in which case
> you 
> can choose to use:
>    C and foo or bar
> or
>    not C and bar or foo
> depending on whether foo or bar is potentially false.
> 
> In the remaining cases, you can use
>    (C and [foo] or [bar])[0]
> if it's important to have short-circuit semantics.

But why not just use (bar,foo)[C] in all cases, regardless of the value
of bar or foo?

Or (bar,foo)[bool(C)] in case C might not be a boolean already.




More information about the Zope3-users mailing list