TN Online TestSamacheer Kalvi practice

Scoping - Study Notes

Share this chapter: Telegram

Chapter Summary

Scoping defines the visibility and lifecycle of variables, parameters, and functions across different regions of a program. Namespaces serve as dictionaries mapping variable names to their respective memory objects. The order of searching for variable resolution is governed by the hierarchal LEGB (Local, Enclosed, Global, Built-in) rule. Additionally, scoping enables encapsulation through access modifiers, which secure program data in modular designs.

Learning Objectives

After studying this chapter, students will be able to:

Key Concepts and Definitions

Scope and Lifetime

Scope refers to the accessibility of a variable in one part of a program to other parts of the same program. Lifetime is the total duration for which a variable remains allocated and active in the memory.

Mapping and Namespaces

Mapping is the process of binding a variable name to an object in memory, typically achieved using the assignment operator. Namespaces are containers or dictionaries that store these variable-to-object bindings to prevent name collisions.

The LEGB Rule

The LEGB rule is the scope search order used for name resolution. It prioritizes the scopes from highest to lowest: Local, Enclosed, Global, and Built-in.

Access Control

Access control is a security technique that regulates who or what can view or use resources in a computing environment. In object-oriented programming, this is implemented using access modifiers to ensure the principle of data encapsulation.

Worked Methods

Applying Name Resolution with the LEGB Rule

When a name is referenced in a program, the compiler or interpreter searches the scopes in a strict sequence:

  1. Local (L): First, check the local scope of the current function or method.
  2. Enclosed (E): If not found locally, search outer enclosing function scopes in nested function blocks from inside out.
  3. Global (G): Next, search the global namespace containing names defined at the top-level of the module.
  4. Built-in (B): Finally, search the built-in namespace, which contains pre-loaded library names and keywords.

Implementing Class-Level Access Control

To control the visibility of variables inside a class, use access specifiers. In languages like C++ and Java, use keywords. In Python, use the naming conventions:

Common Exam Traps

Exam Tips

Solved MCQs → Practice test →

More for this chapter

Book Back Questions10 textbook MCQs · solved Additional MCQs15 extra MCQs · solved Practice TestInteractive · instant score Book Back TestTest yourself on the textbook set Additional MCQ TestTest yourself on the extra set Formula SheetAll key formulas

More chapters in Computer Science

View all
1 Function 2 Data Abstraction 4 Algorithmic Strategies 5 Python -Variables and Operators 6 Control Structures 7 Python functions 8 Strings and String manipulation 9 Lists, Tuples, Sets and Dictionary 10 Python Classes and objects 11 Database Concepts 12 Structured Query Language (SQL) 13 Python and CSV files 14 Importing C++ programs in Python. 15 Data manipulation through SQL 16 Data visualization using pyplot: line chart, pie chart and bar chart