[ZPT] tal:repeat over the sorted keys of a dictionary
Fred L. Drake, Jr.
fred at zope.com
Fri Jul 25 12:52:40 EDT 2003
Jean Jordaan writes:
> Ugh, I think it's sordid to let lie the perfectly apposite 'sort'
> method of dictionaries, in favour of some formatting protocol
> helper :/
Now, a sort() method on dictionaries would be perfectly sordid!
More seriously, the issue here is one of using return values versus
side effects. In Python code, the use of side effects is quite
natural, but it's just not what TAL/TALES were designed for. People
have asked Guido to let [].sort() return the sorted list (as well as
sorting it in place), but that would break a Python consistency rule
in a fundamental way ("if you're calling it for a side effect, don't
return anything"). Since you're relying directly on [].sort() in your
first expression, that's just how it works.
Using lots of helpers via adapters selected by a namespace prefix
could indeed make a template hard to read, but I think they're easier
to read than including a bunch of Python expressions, and much less
obscure than relying on side effects in a templating system that's
remarkably functional (as in functional programming, not simply that
it works).
-Fred
--
Fred L. Drake, Jr. <fred at zope.com>
PythonLabs at Zope Corporation
More information about the ZPT
mailing list