2023-05-03 11:06:20 +02:00
|
|
|
== Why is this an issue?
|
|
|
|
|
2022-11-07 17:23:24 +01:00
|
|
|
Unpacking lets developers assign values of an iterable simultaneously to different targets in a single assignment.
|
|
|
|
However, for this assignment to work, the iterable should have the same number of elements as there are targets in the target list.
|
|
|
|
If this is not respected, a `ValueError` will be produced at runtime.
|
|
|
|
|
2023-05-03 11:06:20 +02:00
|
|
|
=== Noncompliant code example
|
2022-11-07 17:23:24 +01:00
|
|
|
|
|
|
|
[source,python]
|
|
|
|
----
|
|
|
|
def foo(param):
|
|
|
|
ls = [1, 2, 3]
|
|
|
|
x, y = ls # Noncompliant
|
|
|
|
----
|
|
|
|
|
2023-05-03 11:06:20 +02:00
|
|
|
=== Compliant solution
|
2022-11-07 17:23:24 +01:00
|
|
|
|
|
|
|
[source,python]
|
|
|
|
----
|
|
|
|
def foo(param):
|
|
|
|
ls = [1, 2, 3]
|
|
|
|
x, y, z = ls
|
2023-05-25 14:18:12 +02:00
|
|
|
----
|