Do eval() and exec not accept a function definition? (like 'def foo: pass) ?

vasudevram vasudevram at gmail.com
Sat Jun 23 17:09:21 EDT 2007


On Jun 24, 1:20 am, Eduardo Dobay <edudo... at gmail.com> wrote:
> Hey,
>
> I think you could use lambda functions for that matter (Ever heard of
> them?). You could write something like:
>
> def generate_html_tag_function(tag_name, start_or_end):
>    start_or_end.lower()
>    assert(start_or_end in ('start', 'end'))
>    if start_or_end == 'start':
>        function = lambda: '<' + tag_name + '>'
>    else:
>        function = lambda: '</' + tag_name + '>'
>    return function
>
> Then you would create the functions using the same code you had
> written before:
>
> start_html = generate_html_tag_function('html', 'start')
> start_body = generate_html_tag_function('body', 'start')
> end_html = generate_html_tag_function('html', 'end')
> end_body = generate_html_tag_function('body', 'end')
>
> That seems to do what you want.
>
> Eduardo

Thanks to all the repliers.

@Eduardo: Yep, I had heard of lambdas, but didn't think to use them
here.
Will try this out - thanks!

- Vasudev





More information about the Python-list mailing list