|
43 | 43 |
|
44 | 44 | 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.
|
45 | 45 |
|
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. |
47 | 47 |
|
48 | 48 | Assigning a variable `v = 1` at the python REPL (Read Eval Print Loop) prompt triggers the following:
|
49 | 49 |
|
@@ -1027,13 +1027,57 @@ Even though the names within a scope can be accessed as such, it's not suggested
|
1027 | 1027 |
|
1028 | 1028 | ### 2.9. The `import` statement
|
1029 | 1029 |
|
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. |
1031 | 1031 |
|
| 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 | + |
1032 | 1070 | #### 2.9.1. How does `import` work?
|
1033 | 1071 |
|
| 1072 | +The `import` statement |
| 1073 | + |
| 1074 | +The steps can be summarized as: |
| 1075 | + |
1034 | 1076 | 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 | + |
1037 | 1081 |
|
1038 | 1082 | ```python
|
1039 | 1083 | In [12]: import sys
|
@@ -1160,4 +1204,3 @@ NameError Traceback (most recent call last)
|
1160 | 1204 |
|
1161 | 1205 | NameError: name 'func2' is not defined
|
1162 | 1206 | ```
|
1163 |
| - |
|
0 commit comments