Home >Backend Development >Python Tutorial >Why Does Python's Default Argument Mutability Seem to Violate the Principle of Least Astonishment?

Why Does Python's Default Argument Mutability Seem to Violate the Principle of Least Astonishment?

Patricia Arquette
Patricia ArquetteOriginal
2024-12-31 18:14:09925browse

Why Does Python's Default Argument Mutability Seem to Violate the Principle of Least Astonishment?

Default Argument Mutability and the Principle of Least Astonishment

The Python community has long grappled with the "Least Astonishment" principle, which aims to design features that are intuitive and conform to expectations. However, the mutability of default arguments presents a puzzling departure from this principle.

Consider the following function:

def foo(a=[]):
    a.append(5)
    return a

Python novices might anticipate that calling this function without parameters would consistently yield a list with only one element: [5]. However, the actual behavior is far more peculiar:

>>> foo()
[5]
>>> foo()
[5, 5]
>>> foo()
[5, 5, 5]
>>> foo()
[5, 5, 5, 5]

This behavior has drawn criticism from some, who view it as a "dramatic design flaw." However, there is a logical explanation behind it.

Functions in Python are first-class objects, meaning they can be manipulated and assigned to variables like any other type. When a def statement is executed, a function object is created. This object contains not only the function's code but also its default arguments, which are stored as attributes.

By default, default arguments are evaluated at function definition time rather than at function call time. This means that the state of default arguments can change throughout the program's execution, leading to the observed behavior in the foo function.

This design decision stems from the need to maintain consistency. If default arguments were evaluated at function call time, the def line would become a hybrid statement, where part of the binding would occur at both definition and invocation. This approach would introduce additional complexity and potential confusion.

As noted by the effbot, this behavior is not without its practical applications. For instance, consider the following code:

def a():
    print("a executed")
    return []

def b(x=a()):
    x.append(5)
    print(x)

a executed
>>> b()
[5]
>>> b()
[5, 5]

Here, the default value of x is evaluated at function definition time, ensuring that a() is only called once, regardless of how many times b() is invoked.

In conclusion, the mutability of default arguments in Python is an intentional design choice that prioritizes consistency and aligns with the principle of first-class functions. While it may initially seem counterintuitive, it provides greater control and flexibility in function implementation.

The above is the detailed content of Why Does Python's Default Argument Mutability Seem to Violate the Principle of Least Astonishment?. 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