Back to Network AutomationPython fundamentals

Python Scope

Understand where variables are available, how local and global names interact, and why explicit data flow keeps automation scripts easier to maintain.

BeginnerLocal ScopeGlobal ScopeFunctions

Python Scope at a Glance

Python uses scope to decide where a variable name can be accessed. Variables created inside a function are local; variables created outside functions are global.

Local scope

A name created inside a function is available only within that function call.

Global scope

A name created outside a function can be read throughout the module, including inside functions.

Clear data flow

Prefer function arguments and return values to changing global state from inside a function.

In This Lesson

  1. Local Scope
  2. Global Scope
  3. Overlapping Variable Names
  4. Global Variable
  5. Conclusion

1. Local Scope

A variable created inside a function belongs to that function's local scope. The function can use it, but code outside the function cannot.

Python scope infographic comparing local and global scope, overlapping variable names, the global keyword, key takeaways, and network engineering best practices
Python scope at a glance: local and global variables, overlapping names, the global keyword, and maintainable data-flow practices.

Create a local variable

def test_local_scope():
    test_string = "local scope"
    print("executed from function: " + test_string)

test_local_scope()
print("executed globally: " + test_string)

Output

executed from function: local scope
Traceback (most recent call last):
  File "main.py", line 7, in <module>
    print("executed globally: " + test_string)
NameError: name 'test_string' is not defined

The first message runs inside the function. The second print() fails because test_string exists only in the function's local scope.

2. Global Scope

A variable created outside a function belongs to the global scope. A function can read that global variable when no local variable with the same name shadows it.

Read a global variable

test_string = "Global scope."

def test_global_scope():
    print("Executed from function: " + test_string)

test_global_scope()

Output

Executed from function: Global scope.

The function can read test_string. Assigning to that name inside the function is different: unless declared with global, the assignment makes it a local name.

3. Overlapping Variable Names

Using the same variable name globally and inside a function can be confusing. If Python sees an assignment to a name anywhere in a function, it treats that name as local throughout the function unless told otherwise.

Read before local assignment

test_string = "Global scope."

def test_global_scope():
    print("Executed from function: " + test_string)
    test_string = "Local scope."

test_global_scope()

Output

Traceback (most recent call last):
  File "main.py", line 8, in <module>
    test_global_scope()
  File "main.py", line 4, in test_global_scope
    print("Executed from function: " + test_string)
UnboundLocalError: local variable 'test_string' referenced before assignment

The assignment makes test_string local, but the function tries to read it before that local value has been assigned.

Assign the local value first

When the local assignment happens before the read, the local and global variables can exist independently:

Separate local and global values

test_string = "Global scope."

def test_global_scope():
    test_string = "Local scope."
    print("Executed from function: " + test_string)

test_global_scope()
print("Executed globally: " + test_string)

Output

Executed from function: Local scope.
Executed globally: Global scope.

This works because the function uses its own local test_string, while the final statement uses the separate global variable. Avoid reusing the same name when the distinction is not immediately clear.

4. Global Variable

The global keyword tells Python that assignments inside a function should target a name in the global scope.

Create a global variable from a function

def test_global_keyword():
    global test_string
    test_string = "Global variable."

test_global_keyword()
print("Executed globally: " + test_string)

Output

Executed globally: Global variable.

Although this is possible, frequent global modification makes code harder to understand and test. Prefer returning data from the function and assigning the result explicitly:

Prefer a return value

def build_status_message():
    return "Global variable."

test_string = build_status_message()
print("Executed globally: " + test_string)
Network automation practice: Pass hostnames, credentials, command lists, and connection settings into functions as arguments. Return results instead of hiding changes in global state.

5. Conclusion

Variables created inside a function have local scope and are available only within that function. Variables created outside a function belong to the global scope. The global keyword can create or modify a global name from inside a function, but arguments and return values usually produce clearer, safer code.

  • Use local variables for temporary work inside a function.
  • Read global values only when shared module-level data is genuinely useful.
  • Avoid overlapping names when they make the code harder to follow.
  • Prefer explicit inputs and returned results over changing globals.

Python Scope Frequently Asked Questions

What is local scope in Python?

A variable created inside a function has local scope and is normally available only inside that function.

Can a Python function read a global variable?

Yes. A function can read a global variable as long as it does not also assign to that name locally. An assignment causes Python to treat the name as local unless the function declares it with global.

Should I use the global keyword in Python?

Use it sparingly. Function arguments and return values usually make data flow clearer and easier to test.