InterviewTreedInterviewTreed

Programming & Frameworks

Global and nonlocal keywords

Learn how global and nonlocal declarations modify variable bindings in enclosing scopes and when these keywords are appropriate.

Learning guide14 of 85 lessons

Your learning progress is saved to your InterviewTreed account.

1

Introduction to Global and nonlocal keywords

Understand how global and nonlocal declarations modify variable assignment behavior across scopes.

Learn when nested functions need to update variables defined outside their local scope.

2

Core concepts and architecture

global refers to a name in the module-level namespace.

nonlocal refers to a binding in the nearest enclosing function scope.

3

Practical implementation

Use nonlocal in closures that maintain state between function calls.

Use global cautiously when a module-level variable must be reassigned.

4

Challenges and trade-offs

Global state introduces coupling and can make concurrent code harder to reason about.

Nonlocal declarations only work when an appropriate enclosing binding exists.

5

Interview preparation

Differentiate global and nonlocal with code examples.

Explain why modifying a variable inside a function may require a scope declaration.

Key takeaways

  • Understand the purpose and main concepts of Global and nonlocal keywords.
  • Understand how Global and nonlocal keywords works and how its components interact.
  • Be prepared to explain practical implementation and real-world use cases.
  • Understand common challenges, limitations, and design trade-offs.
  • Practise explaining Global and nonlocal keywords clearly in a technical interview.