liveBook · Manning

Global variable

As explained in Software Engineering for Data Scientists

A global variable is a variable accessible from scopes throughout the module or code file where it is defined.

A global variable holds state outside a function, so a function can read or modify it with a global declaration. This can seem convenient in a quick data-processing script, but it makes the function depend on state that is not visible in its inputs or outputs. Prefer keeping data local to a function and returning the result. section 3.1.1

For a churn-data workflow, declaring a dataframe as global lets process_data load, clean, and sample data by changing shared df. A clearer version creates df inside the function, performs the same fill and sample steps, and returns df. Callers can then explicitly receive and use the processed data. section 3.1.1

Listing 3.8 Global variables should generally be avoided in your code.
def process_data(file_name, num_samples): #1

    global df #2
    df = pd.read_csv(file_name) #3

    df = df.fillna(df.median()) #3

    if num_samples > 0:
      df = df.sample(num_samples) #3

def process_data(file_name, num_samples): #4

    df = pd.read_csv(file_name) #5

    df = df.fillna(df.median()) #5

    if num_samples > 0:
      df = df.sample(num_samples) #5

    return df #6

Compare the shared-dataframe version with the version that returns its processed dataframe. The second makes the function’s result explicit without a global declaration. section 3.1.1

Avoid global variables when these practical concerns matter:

  • Memory use: a global variable consumes memory until program execution finishes, so large shared data can remain allocated longer than needed. section 7.6.2
  • Data processing: use functions, or potentially classes, to handle data rather than allocating global variables. section 7.6.2
  • Function updates: pass a value such as num_orders as a parameter and return the updated value instead of modifying a global counter. section 3.1.1

Make a data-processing result explicit

Instead of having process_data modify global df, let it read a file, replace missing values with median values, optionally sample rows, and return df. The caller now holds the resulting dataframe, making the data flow easier to follow. section 3.1.1

Global variables are not always ruled out: a constant is identified as a case where one may be necessary. Otherwise, minimizing globals supports clearer code structure and can avoid keeping data allocated for the full run. section 7.6.2 section 3.1.1

How Software Engineering for Data Scientists covers it

How deep it goes
Demonstrates global dataframe and counter refactors, while deferring scope mechanics and language internals.
Examples
Uses churn-data dataframe processing and an order-count update function to contrast globals with returned values.
What it assumes
Assumes basic Python functions, parameters, variables, and dataframe processing.
What you can do after
Apply it via Python functions by passing state explicitly and returning processed data or updated values.
How it connects
Connects global variables to PEP8, functions, classes, constants, Pandas, and memory management.

Also explained in

Compare how 13 books explain global variable →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Software Engineering for Data Scientists. The section links point to the parts of the book it draws on.