CodeQL documentation

Mismatch in multiple assignment

ID: py/mismatched-multiple-assignment
Kind: problem
Security severity: 
Severity: error
Precision: very-high
Tags:
   - quality
   - reliability
   - correctness
Query suites:
   - python-security-and-quality.qls

Click to see the query in the CodeQL repository

An assignment statement evaluates a sequence expression and assigns each item of the sequence to one of the variables on the left. If there is a mismatch between the number of variables on the left and the values in the sequence on the right of the statement, then an exception is raised at runtime.

Recommendation

Ensure that the number of variables on either side of the assignment match.

Example

The following examples show a simple definition of the Fibonacci series. In the first example, one of the values in the assignment has been duplicated, causing an exception at runtime.

# Fibonacci series 1:
# the sum of two elements defines the next

a, b = 0, 1, 1  # Assignment fails: accidentally put three values on right
while b < 10:
     print b
     a, b = b, a+b

# Fibonacci series 2:
# the sum of two elements defines the next
a, b = 0, 1   # Assignment succeeds: two variables on left and two values on right
while b < 10:
     print b
     a, b = b, a+b

References

  • © GitHub, Inc.
  • Terms
  • Privacy
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy