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.
I had guessed that we can开发者_Python百科 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).
"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']
All these functions are part of module __builtins__
. Fire up a python shell and check the help -
>>> help(__builtins__)
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.
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.
__ builtin __ is imported by default on all program run.
type, str, dir, and all the rest of Python's built-in functions are grouped into a special module called __ builtin __ . (That's two underscores before and after.) If it helps, you can think of Python automatically executing from __ builtin __ import * on startup, which imports all the “built-in” functions into the namespace so you can use them directly.
The advantage of thinking like this is that you can access all the built-in functions and attributes as a group by getting information about the builtin module. And guess what, Python has a function called info. Try it yourself and skim through the list now. We'll dive into some of the more important functions later. (Some of the built-in error classes, like AttributeError, should already look familiar.)
Source and reference : DiveIntoPython
To see all the installed modules you can use the following command in python interpreter.help("modules")
. It will show all the installed modules.If you are looking for inbuilt functions like print
use the command help(__builtins__)
in python shell.
精彩评论