Skip to content

Instantly share code, notes, and snippets.

@BasicWolf
Created June 17, 2015 13:27
Show Gist options
  • Save BasicWolf/a735eb061cb4efc60f04 to your computer and use it in GitHub Desktop.
Save BasicWolf/a735eb061cb4efc60f04 to your computer and use it in GitHub Desktop.
Cache data and pass it to a method
from functools import wraps
def cache(**cached_kwargs):
def wrap(f):
@wraps(f)
def wrapped(self, *args, **kwargs):
kwargs.update(cached_kwargs)
return f(self, *args, **kwargs)
return wrapped
return wrap
class C:
names = ['Alex', 'Laura', 'John', 'Mia']
@cache(a_names=[name for name in names if name.startswith('A')]
def get_names_startwith_a(self, a_names):
return a_names
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment