Skip to content

Bug type: global variables #5

Description

@mashdragon

A bug type I've come across myself has been with forgetting to use the global keyword with global variables. Here's an example:

# A globally accessible list
current_labels = []

def reset_current_labels():
    """ Clears the label list """
    current_labels = []

The bug is that calling reset_current_labels() will not modify current_labels:

>>> current_labels.append('delete me')
>>> current_labels
['delete me']
>>> reset_current_labels()
>>> current_labels
['delete me']

The correct code would be

# A globally accessible list
current_labels = []

def reset_current_labels():
    """ Clears the label list """
    global current_labels
    current_labels = []

So to bug the code, you would remove one or more global statements.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions