Bruno Desthuilliers wrote:
Gabriel Rossetti a écrit :
Terry Reedy wrote:
(snip)
Unlike the class approach, this requires recreating the constant
functions and dict with each call to _test. Quick to write but a
bit 'dirty', in my opinion. Another standard idiom is to set up the
constants outside the function:
def request(params):
pass
def submit(params, values):
pass
def update(params, values):
pass
def delete(params):
pass
dispatch = {'request':request, 'submit':submit, 'update':update,
'delete':delete}
def _test(self, action, *args):
return resultToXmo(dispatch[action](*args))
That's how I had done it originally (before the use of eval()), but
in this case also, since the functions are still nested,
Uh ??? You probably want to re-read the above code snippet.
Uh...yes, I didn't see the external/parent function was no longer there.
I prefer to nest mine though because I have several parent functions for
different tasks, so each child/nested function has a diff.
implementation, I find that cleaner than having n*4+n top-level
functions (+ n dicts), e.g. I prefer this :
def __task1(self, action, *args):
def request(params):
pass
def submit(params, values):
pass
def update(params, values):
pass
def delete(params):
pass
return resultToXml(locals()[action](*args))
def __task2(self, action, *args):
def request(params):
pass
def submit(params, values):
pass
def update(params, values):
pass
def delete(params):
pass
return resultToXml(locals()[action](*args))
over this :
def task1_request(params):
pass
def task1_submit(params, values):
pass
def task1_update(params, values):
pass
def task1_delete(params):
pass
def task2_request(params):
pass
def task2_submit(params, values):
pass
def task2_update(params, values):
pass
def task2_delete(params):
pass
dispatch_task1 = {'request':task1_request, 'submit':task1_submit,
'update':task1_update, 'delete':task1_delete}
dispatch_task2 = {'request':task2_request, 'submit':task2_submit,
'update':task2_update, 'delete':task2_delete}
def _task1(self, action, *args):
return resultToXml(dispatch_task1[action](*args))
def _task2(self, action, *args):
return resultToXml(dispatch_task2[action](*args))
I could use your callable approach, but like you said it may not be
worth the trouble.
Gabriel
--
http://mail.python.org/mailman/listinfo/python-list