Skip to content

CLN: compat.bind_method and core.common.bind_method #10566

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
kawochen opened this issue Jul 14, 2015 · 7 comments
Closed

CLN: compat.bind_method and core.common.bind_method #10566

kawochen opened this issue Jul 14, 2015 · 7 comments
Labels
Milestone

Comments

@kawochen
Copy link
Contributor

I'd like to remove bind_method from here and here, as I don't see any benefit over setattr(cls, name, func). Is there any?

@jreback
Copy link
Contributor

jreback commented Jul 14, 2015

well the doc string explains this
has something changed? (eg maybe it doesn't matter anymore)

@kawochen
Copy link
Contributor Author

The doc string says the first case (PY2) can't be used in PY3, but since the second case can be used in both PY2 and PY3 there's no need to have an if else. Or maybe I'm missing something? In any case I think at least one of the definitions needs to go, as they are almost the same.

@jreback
Copy link
Contributor

jreback commented Jul 14, 2015

see what u do

I don't recall exactly what this is for
prob just a 'nice' interface for the abc stuff

@kawochen
Copy link
Contributor Author

This line in ops.py seems to be the only place it's used.

@jorisvandenbossche
Copy link
Member

There will probably be some reason, as also python-future proposes this for py2/3 compatible code: http://python-future.org/what_else.html#binding-a-method-to-a-class

(but indeed, one of the two implementation can be removed, I propose to keep the one in compat?)

@kawochen
Copy link
Contributor Author

I don't feel strongly about removing bind_method, but I still think it's strange. Methods are stored in cls.__dict__ as functions, but by first constructing with types.MethodType, you store them as unbound methods, so this doesn't imitate defining a method inside a class body well. The typing also means bind_method can't bind a classmethod, but this should be acceptable (not that it is used here), as what matters is whether cm.__get__(cls(), cls) and cm.__get__(None, cls) are callable, not whether cm is callable.

PY2:

>>> class G(object):
...     def g(self): return self
...
>>> G.__dict__['g']
<function g at 0x0000000002C56EB8>
>>> def h(self): return self
...
>>> setattr(G, 'h', types.MethodType(h, None, G))
>>> G.__dict__['h']
<unbound method G.h>
>>> setattr(G, 'h_cm', types.MethodType(classmethod(h), None, G))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: first argument must be callable

@jreback
Copy link
Contributor

jreback commented Aug 20, 2015

closed by #10643

@jreback jreback closed this as completed Aug 20, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants