<p>In Python 2, functions (including lambdas) were allowed to destructure their parameters directly in the signature:</p>
<pre class="lang-python prettyprint-override"><code>def f(x, (y, z)):
    return x + y + z

print f(1, (2, 3))
</code></pre>
<p>There are some other languages which allow this (e.g. Javascript added it in ES6 when destructuring assignments were introduced), but many other languages don't allow this even though they allow destructuring in other contexts; and notably the feature was <em>removed</em> in Python 3. What are some reasons not to allow destructuring of function parameters?</p>

<destructuring>
Disadvantages of allowing destructuring of parameters in function signatures
<p>The rationale for removing this feature from Python is explained in <a href="https://peps.python.org/pep-3113/" rel="nofollow noreferrer">PEP 3113</a>. Five arguments against parameter destructuring in Python are given, paraphrased below:</p>
<ul>
<li>It makes introspection (aka <a href="https://en.wikipedia.org/wiki/Reflective_programming" rel="nofollow noreferrer">reflection</a>) difficult. The <code>inspect</code> module allows Python programs to access information about function signatures at runtime, but if these signatures can contain arbitrary destructuring patterns, then more complex data structures are needed to represent function signatures, and this also complicates the code which makes use of this data.</li>
<li>It doesn't add functionality for non-lambda functions, because the destructuring can instead just be done in an assignment statement at the beginning of the function body.</li>
<li>Destructured parameters aren't supported by other features for function parameters, specifically keyword arguments and type annotations.</li>
<li>They make error messages confusing, since the unpacking happens at the call-site before the function is invoked.</li>
<li>The feature is rarely used; as few as 0.18% of functions in the Python 2 standard library used tuple unpacking in parameters.</li>
</ul>

CC BY-SA 4.0
2023-05-31T23:06:54.047Z
2023-11-04T22:46:54.237Z
2023-11-04T22:46:54.237Z
kaya3
# Disadvantages of allowing destructuring of parameters in function signatures
Posted by: username_0 on 2023-05-31
In Python 2, functions (including lambdas) were allowed to destructure their parameters directly in the signature:
def f(x, (y, z)):
    return x + y + z

print f(1, (2, 3))

There are some other languages which allow this (e.g. Javascript added it in ES6 when destructuring assignments were introduced), but many other languages don't allow this even though they allow destructuring in other contexts; and notably the feature was removed in Python 3. What are some reasons not to allow destructuring of function parameters?
**Tags:** destructuring
---
## 1 answers
### Answer by username_0 on 2023-05-31. Score: 8
The rationale for removing this feature from Python is explained in [PEP 3113](https://peps.python.org/pep-3113/). Five arguments against parameter destructuring in Python are given, paraphrased below:

* It makes introspection (aka [reflection](https://en.wikipedia.org/wiki/Reflective_programming)) difficult. The `inspect` module allows Python programs to access information about function signatures at runtime, but if these signatures can contain arbitrary destructuring patterns, then more complex data structures are needed to represent function signatures, and this also complicates the code which makes use of this data.
* It doesn't add functionality for non-lambda functions, because the destructuring can instead just be done in an assignment statement at the beginning of the function body.
* Destructured parameters aren't supported by other features for function parameters, specifically keyword arguments and type annotations.
* They make error messages confusing, since the unpacking happens at the call-site before the function is invoked.
* The feature is rarely used; as few as 0.18% of functions in the Python 2 standard library used tuple unpacking in parameters.
---
