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

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

Barbara Streisand
Barbara StreisandOriginal
2024-11-02 13:35:30669browse

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 Functions, Unbound Methods, and Bound Methods Differ?
* Unders

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

  • A function can be transformed into an unbound method using a type class construction call or the MethodType constructor.
  • An unbound method can be converted into a bound method by accessing it on a class instance.
  • A bound method cannot be transformed back into an unbound method or a function.

Usage

  • Functions are called like normal functions: f1()
  • Unbound methods are called with an instance of the class as the first argument: C.f1(instance)
  • Bound methods are called on class instances: instance.f1()

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!

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