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

Susan Sarandon
Release: 2024-10-26 07:03:30
Original
282 people have browsed it

  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>
Copy after login

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>
Copy after login

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

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

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>
Copy after login

or using the descriptor protocol:

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

Functions can be transformed into unbound methods manually:

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

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

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

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>
Copy after login

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>
Copy after login

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>
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!