Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Inspecting the Built-in Scope

00:00 In this lesson, we will have a look at the built-in scope.

00:06 The B stands for built-in scope. The built-in scope is automatically loaded by Python when you run a program or script. It contains names that are built into Python, such as keywords, functions, exceptions, and other attributes.

00:23 The built-in scope is implemented as a standard-library module named builtins. Let’s hop over to IDLE and have a look at the built-in scope.

00:35 The built-in scope works a bit different than the local, enclosing, and global scope. While you can define names for these three scopes yourself, the names of the built-in scope are predefined.

00:49 That means under the hood the built-in scope was there all the time, and it will be as long as your Python program runs. All of Python’s built-in objects live in a standard-library module called builtins. So when you run the Python interpreter, these objects of the builtins module are inside of the built-in scope for you available right away. So for example, that’s why you can use functions like the print() function in line 4, 8, and 12 right away without importing anything, and Python knows what to do with it.

01:25 But before actually executing the print() function, Python looks in the local, the enclosing, and the global scope first, and if there is no name named print, then Python finally looks into the built-in scope.

01:41 So strictly speaking, you could define your own variable print, and Python would then use your own variable instead of the built-in print() function.

01:52 But that’s usually bad practice. So whenever you see a keyword, for example, that has a different color in your editor, then it’s not a good idea to use this keyword name as a variable name for yourself because again, Python looks in the end in the built-in scope.

02:10 If a variable with a name like print is defined in a local, enclosing, or global scope before that, then Python will try to use this one first.

02:23 Okay, let’s recap. Whenever you use a name, such as a variable name or a function name, Python searches through different scope levels to determine whether the name exists or not. To resolve a name, Python follows a specific order of scope levels.

02:39 This order of looking names up is called LEGB rule. The LEGB stands for local scope, enclosing scope, global scope, and built-in scope.

02:53 So, seeing the scopes listed peacefully next to each other gives the impression that the scopes are like compartments where variables can live in their own scope happily ever after. Well, that’s not always the case. In the next lesson, we will stir things up a bit, but we’ll also clean up after us.

Avatar image for mallikarjunakamathamva

mallikarjunakamathamva on Dec. 1, 2023

Please, elaborate more about the built-in scope.

Avatar image for Bartosz Zaczyński

Bartosz Zaczyński RP Team on Dec. 1, 2023

@mallikarjunakamathamva Check out the written tutorial for more details:

Become a Member to join the conversation.