Greetings,

I was playing around this piece of example code (written from memory).


def filter_text(key, value):

    def do_nothing(text): return text

    return {'this': call_this,

                  'that': call_that,

                  'what': do_nothing

                 }[key](value)


Is there a way to refactor the code to have the inner do_nothing function be the default action for the dictionary?

The original code was a series of if statements. The alternatives include using a lambda to replace the inner function or a try-except block on the dictionary to return value on KeyError exception.

What's the most pythonic and fastest?

Thank you,

Chris R.

--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to