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

Florent Guillaume fg at nuxeo.com
Wed Feb 1 14:26:24 EST 2006


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.

Florent

-- 
Florent Guillaume, Nuxeo (Paris, France)   Director of R&D
+33 1 40 33 71 59   http://nuxeo.com   fg at nuxeo.com


More information about the Zope3-users mailing list