Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug type: global variables #5

Open
mashdragon opened this issue Jan 10, 2023 · 2 comments
Open

Bug type: global variables #5

mashdragon opened this issue Jan 10, 2023 · 2 comments

Comments

@mashdragon
Copy link
Contributor

mashdragon commented Jan 10, 2023

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.

@furlat
Copy link
Owner

furlat commented Jan 10, 2023

Thanks for the suggestion, would this script satisfy your example ? (some of the bugs are unchecked and might not work )

def using_wrong_variable_scope(script, errors_dict):

@mashdragon
Copy link
Contributor Author

Almost. I think to reproduce the bug, the entire global declaration should be missing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants