Skip to content

Commit 0b2f35e

Browse files
author
arvimal
committed
* Added more details on how import works.
1 parent ccadf24 commit 0b2f35e

File tree

1 file changed

+48
-5
lines changed

1 file changed

+48
-5
lines changed

README.md

+48-5
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343

4444
All initializations (variables, functions, classes, and other instances) done in Python are simply names in the current namespace, that points to an object (a blob with some metadata) in memory.
4545

46-
For example, if a new variable is created, it just points to an object in memory.
46+
For example, if a new variable is created, it just points to an object in memory.
4747

4848
Assigning a variable `v = 1` at the python REPL (Read Eval Print Loop) prompt triggers the following:
4949

@@ -1027,13 +1027,57 @@ Even though the names within a scope can be accessed as such, it's not suggested
10271027

10281028
### 2.9. The `import` statement
10291029

1030-
The `import` statement allows us to bring in a set of features and functions into the current namespace.
1030+
The `import` statement loads the content of a python module (a python source file at a pre-defined location) in memory.
10311031

1032+
The methods defined in the module are thus available in the current namespace.
1033+
1034+
* The `import` keyword is a wrapper around the builtin function `__import__`, defined in `__builtin__`.
1035+
1036+
```python
1037+
In [6]: dir(__builtin__)
1038+
Out[6]:
1039+
['ArithmeticError',
1040+
'AssertionError',
1041+
...
1042+
.....
1043+
'__doc__',
1044+
'__import__',
1045+
'__loader__',
1046+
..
1047+
...
1048+
'vars',
1049+
'zip']
1050+
1051+
In [7]: print(__builtin__.__import__.__doc__)
1052+
__import__(name, globals=None, locals=None, fromlist=(), level=0) -> module
1053+
1054+
Import a module. Because this function is meant for use by the Python
1055+
interpreter and not for general use, it is better to use
1056+
importlib.import_module() to programmatically import a module.
1057+
1058+
The globals argument is only used to determine the context;
1059+
they are not modified. The locals argument is unused. The fromlist
1060+
should be a list of names to emulate ``from name import ...'', or an
1061+
empty list to emulate ``import name''.
1062+
When importing a module from a package, note that __import__('A.B', ...)
1063+
returns package A when fromlist is empty, but its submodule B when
1064+
fromlist is not empty. The level argument is used to determine whether to
1065+
perform absolute or relative imports: 0 is absolute, while a positive number
1066+
is the number of parent directories to search relative to the current module.
1067+
1068+
```
1069+
10321070
#### 2.9.1. How does `import` work?
10331071

1072+
The `import` statement
1073+
1074+
The steps can be summarized as:
1075+
10341076
1. A `import` statement helps to bring in a module into the current namespace.
1035-
2. A module provides a set of features (collectively through one or more python source files)
1036-
3. The `import` statement looks into a pre-defined set of paths, for the file name to be imported.
1077+
2. The `import` statement looks into a pre-defined set of paths, for the file name to be imported.
1078+
3. This file/module is then loaded in memory, to the specific namespace.
1079+
4. The methods defined in the module becomes available in the current namespace.
1080+
10371081

10381082
```python
10391083
In [12]: import sys
@@ -1160,4 +1204,3 @@ NameError Traceback (most recent call last)
11601204

11611205
NameError: name 'func2' is not defined
11621206
```
1163-

0 commit comments

Comments
 (0)