What are default modules in python (which are imported when we run Python as for example "print")
Solution 1
"module" has a particular meaning in Python. Neither "print" nor "addition" is a module.
+
and the other "mathematical functions" are operators, while for
, in
, if
, etc. are keywords, not functions, and so aren't going to be in __builtins__
.
If you actually want to know what modules are loaded when you start Python, look at sys.modules.keys()
:
$ python -i
Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.modules.keys()
['copy_reg', 'sre_compile', 'locale', '_sre', 'functools', 'encodings', 'site',
'__builtin__', 'sysconfig', 'operator', '__main__', 'types', 'encodings.encodings',
'abc', '_weakrefset', 'errno', 'encodings.codecs', 'sre_constants', 're', '_abcoll',
'ntpath', '_codecs', 'nt', '_warnings', 'genericpath', 'stat', 'zipimport',
'encodings.__builtin__', 'warnings', 'UserDict', 'encodings.cp1252', 'sys',
'codecs', 'os.path', '_functools', '_locale', 'signal', 'traceback', 'linecache',
'encodings.aliases', 'exceptions', 'sre_parse', 'os', '_weakref']
Solution 2
All these functions are part of module __builtins__
. Fire up a python shell and check the help -
>>> help(__builtins__)
Solution 3
Python has three scopes:
- local
- global
- builtin
If you're executing the following function:
def foo():
print('bar')
Python looks the for the print
function it in these scopes top-to-bottom. First it checks if there is a local variable in foo
named print
. Then, if that fails, if print
is a global script/module object (in this example foo
function is such an object). If that fails as well, it looks in the __builtin__
module.
The __builtin__
module is where all functions like print
are defined. It also contains builtin exception classes.
Addition and other operators are different because they work on objects. Every object has a type, for example an integer 123
is of type int
. The int
type defines how two integers can be added and what is the result.
In CPython
(Python from python.org), the __builtin__
module and all basic types (int
, str
, list
, etc.) are written in C and are part of the core (the python
executable file). There are other modules that are written the same way, most notably the sys
module. You won't find sys.py
in Python's standard library.
Solution 4
The __builtins__
module contains the print
function, as well as many other functions and classes. In a shell, help(__builtins__)
will give you an almost absurdly-detailed list of functions, classes, and those classes functions. Using dir(__builtins__)
may be more useful, as it simply returns a list of the names of the included functions and classes.
![Md Faisal](https://i.stack.imgur.com/PAaRL.png?s=256&g=1)
Comments
-
Md Faisal about 2 years
What are the default modules in Python which are automatically imported when Python compiler is launched?
These are, for example,
print
, addition, and other mathematical functions.
They must be defined somewhere in PythonI had guessed that we can extract this information by finding (for example from where
print
function has come from if we know this information we can get the module it has come from).-
tripleee over 12 yearsThe fundamentals of Python are implemented in C inside your
/usr/bin/python
.
-
-
ronakg over 12 years
__builtins__
is a module, not a dict so it doesn't havekeys()
method.dir(__builtins__)
should give a list of items that are imported automatically (not considering doc, name etc). -
Md Faisal over 12 yearsthanks for the answer but are they any more like builtin which are imported automatically and one more thing can we find name of module by just having the function names just in case if we dont know which module is importing it
-
Karl Knechtel over 12 yearsSome of them are built into the language, and therefore aren't really anywhere, unless you count some C code that is used to implement Python... and even then, even if you understand the code, it is hard to point to a part of it and say "this is XYZ Python functionality".
-
agf over 12 years@EliBendersky RonakG is right, that
help
is correct. However, if you're actually looking for which modules are imported when you run Python, those are not listed in__builtins__
so if it's an interview question that's the wrong answer. -
ronakg over 12 yearsI didn't know about this. Thanks.
-
Md Faisal over 12 years@agf so then whats the answer?
-
agf over 12 yearsHowever that has nothing to do with what modules are loaded, nor does it cover "addition".