<p>Given a reference to a method, is there a way to check whether the method is bound to an object or not?  Can you also access the instance that it's bound to?</p>

[{'answer_id': 53237, 'author': 'Mark Cidade', 'author_id': 1659, 'author_profile': 'https://Stackoverflow.com/users/1659', 'pm_score': 2, 'selected': False, 'text': '<p><a href="https://docs.python.org/2/reference/datamodel.html?highlight=im_self" rel="nofollow noreferrer"><strong>im_self</strong> attribute</a> (only Python 2)</p>\n'}, {'answer_id': 53322, 'author': 'jfs', 'author_id': 4279, 'author_profile': 'https://Stackoverflow.com/users/4279', 'pm_score': 7, 'selected': True, 'text': '<pre class="lang-py prettyprint-override"><code>def isbound(method):\n    return method.im_self is not None\n    \ndef instance(bounded_method):\n    return bounded_method.im_self\n</code></pre>\n<p><a href="https://docs.python.org/2.7/reference/datamodel.html#index-40" rel="nofollow noreferrer">User-defined methods:</a></p>\n<blockquote>\n<p>When a user-defined method object is\ncreated by retrieving a user-defined\nfunction object from a class, its\n<code>im_self</code> attribute is <code>None</code> and the\nmethod object is said to be unbound.\nWhen one is created by retrieving a\nuser-defined function object from a\nclass via one of its instances, its\n<code>im_self</code> attribute is the instance, and\nthe method object is said to be bound.\nIn either case, the new method\'s\n<code>im_class</code> attribute is the class from\nwhich the retrieval takes place, and\nits <code>im_func</code> attribute is the original\nfunction object.</p>\n</blockquote>\n<p>In Python <a href="https://docs.python.org/2.7/whatsnew/2.6.html" rel="nofollow noreferrer">2.6 and 3.0</a>:</p>\n<blockquote>\n<p>Instance method objects have new\nattributes for the object and function\ncomprising the method; the new synonym\nfor <code>im_self</code> is <code>__self__</code>, and <code>im_func</code>\nis also available as <code>__func__</code>. The old\nnames are still supported in Python\n2.6, but are gone in 3.0.</p>\n</blockquote>\n'}, {'answer_id': 18955425, 'author': 'Rob Agar', 'author_id': 494373, 'author_profile': 'https://Stackoverflow.com/users/494373', 'pm_score': 5, 'selected': False, 'text': "<p>In python 3 the <code>__self__</code> attribute is <em>only</em> set on bound methods. It's not set to <code>None</code> on plain functions (or unbound methods, which are just plain functions in python 3). </p>\n\n<p>Use something like this:</p>\n\n<pre><code>def is_bound(m):\n    return hasattr(m, '__self__')\n</code></pre>\n"}, {'answer_id': 50074581, 'author': 'Klaus', 'author_id': 1479700, 'author_profile': 'https://Stackoverflow.com/users/1479700', 'pm_score': 4, 'selected': False, 'text': "<p>The chosen answer is valid in almost all cases. However when checking if a method is bound in a decorator using chosen answer, the check will fail. Consider this example decorator and method:</p>\n\n<pre><code>def my_decorator(*decorator_args, **decorator_kwargs):\n    def decorate(f):\n        print(hasattr(f, '__self__'))\n        @wraps(f)\n        def wrap(*args, **kwargs):\n            return f(*args, **kwargs)\n        return wrap\n    return decorate\n\nclass test_class(object):\n    @my_decorator()\n    def test_method(self, *some_params):\n        pass\n</code></pre>\n\n<p>The <code>print</code> statement in decorator will print <code>False</code>.\nIn this case I can't find any other way but to check function parameters using their argument names and look for one named <code>self</code>. This is also <strong>not</strong> guarantied to work flawlessly because the first argument of a method is not forced to be named <code>self</code> and can have any other name.</p>\n\n<pre><code>import inspect\n\ndef is_bounded(function):\n    params = inspect.signature(function).parameters\n    return params.get('self', None) is not None\n</code></pre>\n"}, {'answer_id': 65276709, 'author': 'Anakhand', 'author_id': 6117426, 'author_profile': 'https://Stackoverflow.com/users/6117426', 'pm_score': 0, 'selected': False, 'text': '<p>A solution that works for both Python 2 and 3 is tricky.</p>\n<p>Using the package <a href="https://six.readthedocs.io/" rel="nofollow noreferrer"><code>six</code></a>, one solution could be:</p>\n<pre class="lang-py prettyprint-override"><code>def is_bound_method(f):\n    &quot;&quot;&quot;Whether f is a bound method&quot;&quot;&quot;\n    try:\n        return six.get_method_self(f) is not None\n    except AttributeError:\n        return False\n</code></pre>\n<p>In Python 2:</p>\n<ul>\n<li>A regular function won\'t have the <code>im_self</code> attribute so <code>six.get_method_self()</code> will raise an <code>AttributeError</code> and this will return <code>False</code></li>\n<li>An unbound method will have the <code>im_self</code> attribute set to <code>None</code> so this will return <code>False</code></li>\n<li>An bound method will have the <code>im_self</code> attribute set to non-<code>None</code> so this will return <code>True</code></li>\n</ul>\n<p>In Python 3:</p>\n<ul>\n<li>A regular function won\'t have the <code>__self__</code> attribute so <code>six.get_method_self()</code> will raise an <code>AttributeError</code> and this will return <code>False</code></li>\n<li>An unbound method is the same as a regular function so this will return <code>False</code></li>\n<li>An bound method will have the <code>__self__</code> attribute set (to non-<code>None</code>) so this will return <code>True</code></li>\n</ul>\n'}]
['https://Stackoverflow.com/questions/53225', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4883/']