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

Eduardo Dobay edudobay at
Sat Jun 23 22:20:34 CEST 2007


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):
   assert(start_or_end in ('start', 'end'))
   if start_or_end == 'start':
       function = lambda: '<' + tag_name + '>'
       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.


More information about the Python-list mailing list