Home >Backend Development >Python Tutorial >What\'s the Difference Between Functions, Unbound Methods, and Bound Methods in Python?

What\'s the Difference Between Functions, Unbound Methods, and Bound Methods in Python?

Susan Sarandon
Susan SarandonOriginal
2024-10-26 07:03:30357browse

  What's the Difference Between Functions, Unbound Methods, and Bound Methods in Python?

Unveiling the Differences Between Functions, Unbound Methods, and Bound Methods

Consider the following code snippet:

<code class="python">class A(object):
    def f1(self): pass
a = A()</code>

The variable f1 can manifest itself in three distinct forms:

  • Bound Method: a.f1 provides access to the method associated with the instance a.
  • Unbound Method: A.f1 corresponds to the method attached to the class A.
  • Function: a.__dict__['f1'] directly retrieves the underlying function definition.

Object Distinctions

A function, defined by the def keyword or lambda, undergoes a transformation when placed within a class statement. In Python 2, this transformation creates an unbound method, a concept absent in Python 3. Accessing this method on an instance of the class further transforms it into a bound method, which seamlessly binds the instance as the initial parameter (self).

Example:

<code class="python">def f1(self):
    pass</code>

Here, f1 is a function. In contrast, C.f1 is an unbound method:

<code class="python">class C(object):
    f1 = f1</code>

Method Invocation and Transformation

Unbound methods can be converted to bound methods by accessing them on an instance of their class type:

<code class="python">C().f1</code>

or using the descriptor protocol:

<code class="python">C.f1.__get__(C(), C)</code>

Functions can be transformed into unbound methods manually:

<code class="python">import types
types.MethodType(f1, None, C)</code>

Combining these techniques allows for the direct creation of bound methods:

<code class="python">types.MethodType(f1, None, C).__get__(C(), C)</code>

The crucial distinction between functions and unbound methods lies in the latter's awareness of its class binding. Hence, invoking or binding an unbound method requires an instance of its affiliated class type.

In Python 3, the distinction between functions and unbound methods is eliminated. Instead, accessing a function on a class instance directly returns the function itself:

<code class="python">C.f1 is f1</code>

Method Equivalency

In summary, the following invocations are equivalent in both Python 2 and Python 3:

<code class="python">f1(C())
C.f1(C())
C().f1()</code>

Binding a function to an instance effectively fixes its initial parameter to the instance, making the bound method analogous to the following lambda expression:

<code class="python">lambda *args, **kwargs: f1(C(), *args, **kwargs)</code>

The above is the detailed content of What\'s the Difference Between Functions, Unbound Methods, and Bound Methods in Python?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn