Re: Have do_nothing as default action for dictionary?
Greetings, After reading everyone's comments and doing a little more research, I re-implemented my function as a callable class. def __call__(self, key, value): if key not in self._methods: return value return self._methods[key](value) This behaves like my previous function, solved the problem that I had with the dictionary, the dictionary is created only once, a half dozen functions got moved into the new class, and the old class now has less clutter. Thanks everyone! Chris R. -- https://mail.python.org/mailman/listinfo/python-list
Re: Have do_nothing as default action for dictionary?
Christopher Reimer via Python-list 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? If it does nothing, why invoke it at all? LOOKUP = {"this": call_this, ...} def filter_text(key, value): if key in LOOKUP: return LOOKUP[key](value) return value If there are much more hits than misses: def filter_text(key, value): try: process = return LOOKUP[key] except KeyError: return value return process(value) If you insist on invoking a noop func: def do_nothing(text): return text def filter_text(key, value): return LOOKUP.get(key, do_nothing)(value) With a collections.defaultdict (will grow to comprise new keys): LOOKUP = defaultdict(LOOKUP, lambda: do_nothing) def filter_key(key, value): return LOOKUP[key](value) > 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
Re: Have do_nothing as default action for dictionary?
Christopher Reimer via Python-list 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? If it does nothing, why invoke it at all? LOOKUP = {"this": call_this, ...} def filter_text(key, value): > 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
Re: Have do_nothing as default action for dictionary?
On Mon, Sep 4, 2017 at 5:31 AM, Christopher Reimer via Python-listwrote: > 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
Have do_nothing as default action for dictionary?
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