Inspecting 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.
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.
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
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: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: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.
Become a Member to join the conversation.