This Python module provides direct access to all ‘built-in’ identifiers of Python. For example, builtins.open is the full name for the built-in function
open(). This module is not normally accessed explicitly by most applications, but can be useful in modules that provide objects with the same name as a built-in value, but in which the built-in of that name is also needed.
For example, in a module that wants to implement an
open() function that wraps the built-in open(), this module can be used directly:
But in cases where there is a function with the same name as the builtin functions these needs to be called explicitly:
def open(path): f = builtins.open(path, 'r')
Here the first open function is defined by the programmer and the second one is being imported from the builtins module.
One more interesting thing to keep in mind is that the compiler gives higher priority to user-defined versions of the predefined functions contained by the builtins module. So, if a program consists of a call to both types of programs, the user-defined program will be called. To call the predefined version builtins keyword should be used.
calling for pre-defined version of pow() from builtins module to calculate 2**3
inside user-defined function pow() to calculate 2**3
Contents of Builtins Module
This module is automatically loaded every time the interpreter starts, this is the reason why it is generally never called explicitly. The following are defined within builtins:
- The Object Class– base class for all python objects
- All Built-in Data-Type Class– all data types like numbers, string, list etc.
- Built-in Exception Classes– like BaseException class, etc.
- Built-in Functions– functions like open(), abs(), all(), etc.
- Built-in Constants– constants like False, True, etc.
Built-in Data Type Classes
- TRUTH VALUE TESTING- statements and conditions for checking the validity of the code.
- BOOLEAN OPERATIONS- and, or, not.
- COMPARISONS- =, ==, !=, is and is not.
- NUMERIC TYPES- int, float, complex and bitwise operations like and, or, exclusive or, shifts and inverse.
- ITERATOR TYPES- operations related to iteration of a container.
- SEQUENCE TYPES- list, tuple, range and operations on these.
- TEXT SEQUENCE TYPE- str and methods related with string mutation and display.
- BINARY SEQUENCE TYPE- bytes, bytearray and memoryview.
- SET TYPES- set and frozenset.
- MAPPING TYPES- dict.
- CONTEXT MANAGER TYPES- runtime context related matter.
- OTHERS – module, instances, null object,
Example: simple program to depict the application of builtin types
not found not found found not found
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course