Normally I would use inspect.getargspec, however how do I get the arguments of a method that has been bound?
Eg, how do I get the argument names for the method ‘foo’ as follows:
class Foo(object):
@memoized
def foo(self, arg1, arg2):
pass
Note that Foo().foo is a memoized object, http://wiki.python.org/moin/PythonDecoratorLibrary#Memoize
This means that it is really a functools.partial instance.
How do I get the original function or alternatively, obtain the arguments somehow?
If I can’t, does this indicate a design flaw of the PythonDecoratorLibrary?
You could add a _wrapped attribute to the partial, which may be what you did already:
Or you could return self.func instead of the partial if obj is None (i.e. if it’s accessed from the class instead of an instance):
The partial’s
funcattribute is the memoized object’s__call__method. If you call it from a Foo instance, then the first argument is set to the instance obj by the partial (see this in Foo().foo.args). Then inmemoized.__call__, self.func(*args) works like a poor man’s bound method.For the lru_cache, Raymond Hettinger has Python 2 compatible implementations available as ActiveState Code Recipes. There’s also a version for least frequently used, in addition to least recently used.