MAINT: test, refactor design of recursive closures#11910
MAINT: test, refactor design of recursive closures#11910eric-wieser merged 3 commits intonumpy:masterfrom
Conversation
f9c724e to
75de762
Compare
|
I'm not sure this is an improvement for clarity, although good job finding the other cases. Thoughts, @ahaldane? Relates to https://bugs.python.org/issue4921, for anyone reading this who hasn't already found that. |
|
A possible alternative: class recursive(object):
def __init__(self, func):
self.func = func
def __call__(self, *args, **kwargs):
return self.func(self, *args, **kwargs)used as @recursive
def block_recursion(self, arrays, depth=0):
...
self(arrays[i], ...)
block_recursion(arrays) |
|
def recursive(f):
return f.__get__(f)
Edit: This doesn't work |
| return False | ||
|
|
||
|
|
||
| class recursive(object): |
There was a problem hiding this comment.
This could do with a comment about why it is needed
eric-wieser
left a comment
There was a problem hiding this comment.
Looks like an improvement to me. Let's wait for one more opinion, since @recursive was my idea and might be an opinionated one.
|
I like this much more than the magical Perhaps a slight quibble is that I might call it something more specific, like |
|
|
||
| ''' | ||
| def __init__(self, func): | ||
| self.func = func |
There was a problem hiding this comment.
nit: this should probably call functools.update_wrapper
|
Whatever, there's little point updating the wrapper if its a local variable anyway. Thanks @mattip! |
In looking at
flake8 --select F811local variable reuse, I stumbled across the solution in PR #10621 to the problem of recursive closures (#10620). It seems we can avoid assigning the closure to None by using the design pattern in the last comment to that PR.I also added a test for the use of recursive closures in
loadtxt, which exposed another place where we still had cyclical references.