GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • CodeQL documentation
  • CodeQL overview
  • Writing CodeQL queries
  • CodeQL language guides
  • QL language reference
  • CodeQL standard-libraries
  • CodeQL query help
  • CodeQL repository
  • QL publications
  • CodeQL query help for C and C++
  • CodeQL query help for C#
  • CodeQL query help for GitHub Actions
  • CodeQL query help for Go
  • CodeQL query help for Java and Kotlin

This view

  • Original page
  • New address

GeezerVison is showing codeql.github.com — Potentially uninitialized local variable — CodeQL query help documentation

Potentially uninitialized local variable¶

ID: py/uninitialized-local-variable
Kind: problem
Security severity: 
Severity: error
Precision: medium
Tags:
   - quality
   - reliability
   - correctness
Query suites:
   - python-security-and-quality.qls

Click to see the query in the CodeQL repository

This local variable may be used before it is defined. If a variable is assigned to in a function and not explicitly declared global or nonlocal then it is assumed to be a local variable. If it is used before it is defined then an UnboundLocalError will be raised.

Recommendation¶

Review the code and consider the intended scope of the variable. Determine whether the variable should be global or local in scope. If a global variable is required then add a global statement, or in Python 3 you can use a nonlocal statement if the variable occurs in an enclosing function. Otherwise, ensure that the variable is defined before it is used.

Example¶

The following code includes different functions that use variables. test1() fails with an UnboundLocalError because the local variable var is used before it is initialized.

def test():
    var = 1 
    def print_var():
        print var      # Use variable from outer scope
    print_var()
    print var 


def test1():
    var = 2 
    def print_var():
        print var       # Attempt to use variable from local scope. 
        var = 3         # Since this is not initialized yet, this results
    print_var()         # in an UnboundLocalError
    print var 


def test2():
    var = 2 
    def print_var():
        var = 3         # Initialize local version of the variable
        print var       # Use variable from local scope.
    print_var()         # Note that this local variable "shadows" the variable from
    print var           # outer scope which makes code more difficult to interpret.


def test3():
    var = 4
    def print_var():
        nonlocal var    # Use non-local variable from outer scope.
        print var
    print_var()
    print var

References¶

  • Python Standard Library: Built-in Exceptions: UnboundLocalError.

  • Python Frequently Asked Questions: Why am I getting an UnboundLocalError when the variable has a value?.

  • Python Course: Global and Local Variables.

  • Python Language Reference: The global statement, The nonlocal statement.

  • © GitHub, Inc.
  • Terms
  • Privacy
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.