msg178964 - (view) |
Author: Wojciech Danilo (wdanilo) |
Date: 2013-01-03 16:41 |
Hi! I think this behaviour is bug. Lets concider the following code: import inspect class X(object): def a(self):pass def b(self):pass def c(self):pass print(inspect.getmembers(X, predicate=inspect.ismethod)) print(inspect.getmembers(X, predicate=inspect.isfunction)) In python 2.7, the results are: [('a', ), ('b', ), ('c', )] [] and in Python 3.2: [] [('a', <function a at 0x1b0fd10>), ('b', <function b at 0x1b0fe20>), ('c', <function c at 0x1b160d8>)] I think, the results from python 2.7 are correct. |
|
|
msg178966 - (view) |
Author: Ezio Melotti (ezio.melotti) *  |
Date: 2013-01-03 16:56 |
I think that's expected and by design. In Python 3 there are no unbound methods, but simply functions: >>> class X: ... def add(a, b): return a+b ... >>> add = X.add >>> add <function add at 0xb740d26c> >>> add(3, 4) 7 >>> def add(a, b): return a+b ... >>> add <function add at 0xb740d22c> >>> add(3, 4) 7 As you can see there's no real difference between the two "add". It's different though with bound methods (obtained from an instance rather than a class): >>> add = X().add >>> add <bound method X.add of <__main__.X object at 0xb740e0ec>> The documentation is also clear that ismethod() "Return true if the object is a bound method written in Python.". Maybe an additional note can be added to state that "unbound methods" are not included, and that are instead recognized by isfunction(). |
|
|
msg181937 - (view) |
Author: Greg Couch (gregcouch) |
Date: 2013-02-12 00:07 |
In my opinion, the Python 2.7 results are wrong. In Python 2.7, inspect.ismethod returns True for both bound and unbound methods -- ie., is broken according to the documentation. As a workaround, I'm using: def is_bound_method(obj): return hasattr(obj, '__self__') and obj.__self__ is not None is_bound_method also works for methods of classes implemented in C, e.g., int: >>> a = 1 >>> is_bound_method(a.__add__) True >>> is_bound_method(int.__add__) False But is not very useful in that case because inspect.getargspec does not work for functions implemented in C. is_bound_method works unchanged in Python 3, but as noted above, in Python 3, inspect.ismethod properly distinguishes between bound and unbound methods, so it is not necessary. |
|
|
msg183439 - (view) |
Author: Thomas Kluyver (takluyver) * |
Date: 2013-03-04 12:36 |
I agree that the docs for inspect.ismethod() for Python 2 are wrong. The docs say: "Return true if the object is a bound method written in Python." However, it also returns True for an unbound method: >>> class A: ... def meth(self): ... pass ... >>> A.meth >>> import inspect >>> inspect.ismethod(A.meth) True |
|
|
msg183481 - (view) |
Author: Ezio Melotti (ezio.melotti) *  |
Date: 2013-03-04 18:00 |
I checked the tests on 2.7 and found this: # contrary to spec, ismethod() is also True for unbound methods # (see #1785) self.assertIn(('f', B.f), inspect.getmembers(B, inspect.ismethod)) #1785 also has some discussion about this. |
|
|
msg261557 - (view) |
Author: Anna Koroliuk (Anna Koroliuk) * |
Date: 2016-03-11 10:25 |
This patch fixes Python 2.7. |
|
|
msg261558 - (view) |
Author: Roundup Robot (python-dev)  |
Date: 2016-03-11 10:28 |
New changeset 813a0e0934ce by Victor Stinner in branch '2.7': Fix inspect.ismethod() doc https://hg.python.org/cpython/rev/813a0e0934ce |
|
|
msg261559 - (view) |
Author: STINNER Victor (vstinner) *  |
Date: 2016-03-11 10:30 |
Thanks Anna, I pushed your doc fix. Can you please sign the Python Contributor Agreement? https://www.python.org/psf/contrib/ |
|
|
msg261560 - (view) |
Author: Roundup Robot (python-dev)  |
Date: 2016-03-11 10:32 |
New changeset a90b39aa6af4 by Victor Stinner in branch '2.7': Issue #16851: Add Anna Koroliuk to Misc/ACKS https://hg.python.org/cpython/rev/a90b39aa6af4 |
|
|
msg267304 - (view) |
Author: Jelle Zijlstra (JelleZijlstra) *  |
Date: 2016-06-04 19:42 |
Sounds like this can be closed? |
|
|