How to Declare Multiple Global Variables Inside the Scope While Adhering to PEP8 Guidelines

158 views Asked by At

I'm curious about how to declare multiple global variables while adhering to PEP8 guidelines, which recommend limiting each line to a maximum of 79 characters.

According to the global statement documentation defined by the BNF notation statement in the documentation, it seems that there's no built-in support for multi-line declarations like this:

VAR1 = 0
VAR2 = 0
VAR3 = 0
VAR4 = 0

def change(VAR):
    global (VAR1, VAR2,
           VAR3, VAR4)
    pass

So, if I have many global variables to declare, do I have to split them across multiple lines like this?:

def change(VAR):
    global VAR1, VAR2
    global VAR3, VAR4
    pass

I would appreciate any insights or alternative approaches to declaring multiple global variables in line with PEP8 recommendations.

Find the documentation. Hope someone can answer the proble.

5

There are 5 answers

0
Woodford On

That's pretty awkward with so many global variables. Since these globals are settings or configuration values it might be better to move them into their own module and import them:

# settings.py
VAR1 = 1
VAR2 = 2
...
# app.py
import settings

def change():
    foo = settings.VAR1
    ...

Alternatively, combine the values into a collection such as a dict or list:

settings = {
    'var1': 1,
    'var2': 2,
    ...
}

def change():
    global settings

    foo = settings['var1']
    ...
0
jsbueno On

You can either use the line-continuation token, indicated by a \\ character at the end of the line, or use multiple global statements.

This is not something done frequently, and that is the reason parenthesized support was not added to global and nonlocal statements.

0
JonSG On

Here is a potential alternative, though I would be loath to do this myself.

def foo():
    global \
        a, b, \
        c, d
    a = a + b + c + d

a = b = c = d = 1
print(a)
foo()
print(a)
0
alec_djinn On

The 79-char limit is a soft limit. It is ok if you use 90 chars every now and then.

There is nothing wrong with:

def change(VAR):
    global VAR1, VAR2, VAR3, VAR4, VAR5, VAR6

If you really have a long list of variables, consider collecting them in a dictionary or other data structures.

This is a dummy example:

my_globals = dict(
    VAR1 = 'asdads',
    VAR2 = '45345',
    VAR3 = 'jghjh',
    VAR4 = 'rwe',
    VAR5 = '978798',
)


def change(name, value):
    global my_globals
    try:
        assert name in my_globals
        my_globals[name] = value
    except AssertionError:
        print(f'{name} not in my_gobals')

print(my_globals['VAR1']) #asdads
change('VAR1', 'I am a new value') 
print(my_globals['VAR1']) #I am a new value
change('ABC', 'I am a new value') #ABC not in my_gobals

But the best you can do is to rethink your design. Settings go in settings files, not global variables. Using globals is most of the time not needed. Instead, you can explicitly pass the variable instead.

1
sethdhanson On

Another option is to create a class with the variables and just access them as class attributes.

class C:
    VAR1 = 'asdads'
    VAR2 = '45345'
    VAR3 = 'jghjh'
    VAR4 = 'rwe'
    VAR5 = '978798'

def change():
   C.VAR1 = 'new_value'