From 51d18937afacf3147b05a2108fcada77ef770813 Mon Sep 17 00:00:00 2001 From: Matthew Honnibal Date: Tue, 10 Oct 2017 02:21:28 +0200 Subject: [PATCH] Partially apply doc/span/token into method We want methods to act like they're "bound" to the object, so that you can make your method conditional on the `doc`, `span` or `token` instance --- like, well, a method. We therefore partially apply the function, which works like this: ``` def partial(unbound_method, constant_arg): def bound_method(*args, **kwargs): return unbound_method(constant_arg, *args, **kwargs) return bound_method --- spacy/tokens/underscore.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/spacy/tokens/underscore.py b/spacy/tokens/underscore.py index 66c54d6d6..0b10e7afe 100644 --- a/spacy/tokens/underscore.py +++ b/spacy/tokens/underscore.py @@ -1,3 +1,5 @@ +import functools + class Underscore(object): doc_extensions = {} span_extensions = {} @@ -22,7 +24,7 @@ class Underscore(object): if getter is not None: return getter(self._obj) elif method is not None: - return method + return functools.partial(method, self._obj) else: return self._doc.user_data.get(self._get_key(name), default)