Andrew Grover wrote:
> I think I'm following the meat of this very interesting page
> (http://trac.turbogears.org/turbogears/wiki/PassingArgumentsToCallables),
> except for this:
>
> def make_list(c, none=None, *args, **kwargs):
> def _call():
> return [ [], [('', none)] ][none is not None] + \
> c.build_list(*args, **kwargs)
> return _call
>
> I'm having trouble parsing all that punctuation and seeing the point
> of the "none" parameter.
Another possible way to write it is:
def make_list(c, empty_option=None, *args, **kwargs):
def _call():
if empty_option is None:
options = []
else:
options = [('', empty_option)]
return options + c.build_list(*args, **kwargs)
return _call
Using indexing as a shortcut for branching is a common Python idiom. The
above example is a replacement for the ternary operator :
result = ("result_if_false", "result_if_true")[test]
> Why isn't this function as follows?
>
> def make_list(c, *args, **kwargs):
> def _call():
> return c.build_list(*args, **kwargs)
> return _call
This would of course work, but without the possibility to start the
options list with a default 'empty option' first item - which is a
pretty common need.
HTH
--
bruno desthuilliers
développeur
[EMAIL PROTECTED]
http://www.modulix.com
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"TurboGears" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/turbogears
-~----------~----~----~----~------~----~------~--~---