On Mon, Sep 4, 2017 at 5:31 AM, Christopher Reimer via Python-list <python-list@python.org> wrote: > 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?
Easy: use the .get() method. return {'this': call_this, 'that': call_that, }.get(key, do_nothing)(value) ChrisA -- https://mail.python.org/mailman/listinfo/python-list