I am trying to write a test that checks if a variable holding a bound class method is the same as another reference to this method. This is usually not a problem, but it does not seem to work when executed in another method of the same class. Here is a minimal example:
class TestClass: def sample_method(self): pass def test_method(self, method_reference): print(method_reference is self.sample_method)
I really use assert instead of print , but this is neither here nor there, since the end result is the same. The test is performed as follows:
instance = TestClass() instance.test_method(instance.sample_method)
The result is False , although I expect it to be True . The problem manifests itself in both Python 3.5 and Python 2.7 (works under Anaconda).
I understand that related methods are closures that get by doing something like TestClass.test_method.__get__(instance, type(instance)) . However, I would expect that self.sample_method already a reference to such a closure, so that self.sample_method and instance.sample_method represent the same link.
Part of what confuses me is the result of a real pytest test that I run (working on PR for matplotlib ):
assert <bound method TestTransformFormatter.transform1 of <matplotlib.tests.test_ticker.TestTransformFormatter object at 0x7f0101077b70>> is <bound method TestTransformFormatter.transform1 of <matplotlib.tests.test_ticker.TestTransformFormatter object at 0x7f0101077b70>> E + where <bound method TestTransformFormatter.transform1 of <matplotlib.tests.test_ticker.TestTransformFormatter object at 0x7f0101077b70>> = <matplotlib.ticker.TransformFormatter object at 0x7f0101077e10>.transform E + and <bound method TestTransformFormatter.transform1 of <matplotlib.tests.test_ticker.TestTransformFormatter object at 0x7f0101077b70>> = <matplotlib.tests.test_ticker.TestTransformFormatter object at 0x7f0101077b70>.transform1
If I understand the conclusion correctly, the actual comparison (first line) really compares the same objects, but somehow False appears. The only thing I can imagine at this point is that __get__ is actually called twice, but I donβt know why / where / how, and how to get around it.
source share