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

Michael Dudzik mdudzikml at gmail.com
Wed Feb 1 13:40:14 EST 2006


On Wed, 01 Feb 2006 19:15:13 +0100, "Igor Stroh" <igor at rulim.de> said:
> Michael Dudzik wrote:
> > On Wed, 01 Feb 2006 09:07:17 +0100, "Andreas Jung"
> [..]
> >>>in Zope2 there's a test() method that can be used in TAL (*). Is there
> >>>something comparable in Zope3?
> >>>
> >>>I know how to write such a method but how to make it available in the
> >>>namespace of TALES-"python:"-expressions? I would like to write
> >>>expressions like this:
> >>>
> >>><div tal:attributes="class python: 'foo' + test(condition == True,'
> >>>bar','')" />
> >>>
> >>You can write this in Python as
> >>
> >>    condition and true_expression or false_expression
> > 
> > 
> > That doesn't work under some conditions:
> > 
> > C and A or B where C is a boolean
> > 
> > if C is false it evaluates to B
> > if C is true it evaluates to A EXCEPT when bool(A) == false
> > 
> > example:
> > 
> > C and None or 7 ALWAYS evaluates to 7
> 
> Just make it 'not C and 7 or None' :)

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.



More information about the Zope3-users mailing list