Home > Article > Backend Development > Here are some question-based titles that fit the article: Focusing on the Key Distinction: * Functions, Unbound Methods, and Bound Methods in Python: What\'s the Difference? * Python: How do Functio
Understanding the Differences Between Functions, Unbound Methods, and Bound Methods
Understanding the distinction between functions, unbound methods, and bound methods is crucial for programming in Python. This guide unravels the differences, explains how they are transformed, and provides examples to illustrate their usage.
Functions
Functions are defined with the def statement or lambda. They are stand-alone statements that perform specific operations.
Unbound Methods
Under Python 2, functions defined within a class body are transformed into unbound methods. They are still functions but have an implicit first parameter, which is the class.
Bound Methods
When an unbound method is accessed on a class instance, it is converted into a bound method. The class instance is automatically passed as the first self parameter to the method.
Transformation
Usage
Example
Consider the following code:
<code class="python">def f1(self): pass class C(object): f1 = f1</code>
Here, f1 is a function, C.f1 is an unbound method, and C().f1 is a bound method.
Python 3 Distinction
Python 3 eliminates the concept of unbound methods. Functions accessed on class instances are simply returned as the original function.
The above is the detailed content of Here are some question-based titles that fit the article: Focusing on the Key Distinction: * Functions, Unbound Methods, and Bound Methods in Python: What\'s the Difference? * Python: How do Functio. For more information, please follow other related articles on the PHP Chinese website!