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

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


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




More information about the Python-list mailing list