
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Built-in objects in Python (builtins)
The builtins module is automatically loaded every time Python interpreter starts, either as a top level execution environment or as interactive session. The Object class, which happens to be the base class for all Python objects, is defined in this module. All built-in data type classes such as numbers, string, list etc are defined in this module. The BaseException class, as well as all built-in exceptions, are also defined in it. Further, all built-in functions are also defined in the built-ins module.
Since this module is imported in the current session automatically, normally it is not imported explicitly. All the built-in functions used in the executable code are by default considered to be from built-ins module. For example
>>> len('hello') 5
is implicitly equivalent to
>>> import builtins >>> builtins.len('hello') 5
However, explicit import of this module is required when there is also a user defined function of the same name as built-in function. Python interpreter gives higher precedence to user defined function. Hence, if the code contains both user defined as well as built-in function of the same name, the latter must be prefixed with built-ins module.
def len(string): print ('local len() function') print ('calling len() function in builtins module') import builtins l = builtins.len(string) print ('length:',l) string = "Hello World" len(string)
Output
local len() function calling len() function in builtins module length: 11
Most modules have the name __builtins__ made available as part of their globals. The value of __builtins__ is normally either this module or the value of this module’s __dict__attribute.
>>> import math >>> globals() {'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <class '_frozen_importlib.BuiltinImporter'>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, 'math': <module 'math' (built-in)>}
- Related Articles
- Built-in functions supported by dictionary view objects
- In-built Data Structures in Python
- Built-in String Methods in Python
- Built-in Tuple Functions in Python
- Built-In Class Attributes in Python
- Built-in List Functions & Methods in Python
- Built-in Dictionary Functions & Methods in Python
- What are built-in class attributes in Python?
- How to access Python objects within objects in Python?
- What does built-in class attribute __dict__ do in Python?
- What does built-in class attribute __doc__ do in Python?
- What does built-in class attribute __module__ do in Python?
- What does built-in class attribute __bases__ do in Python?
- What are the common built-in data types in Python?
- Timer objects in Python
