I know how to use both, but I'm curious why the decision was made to make one a statement and the other a function.
Asked
Active
Viewed 287 times
10
-
Never thought about that. – linusg Apr 25 '16 at 13:24
-
because the `import` statement benefits from alternate syntax like `from __ import __ as __` where as `reload` only ever needs the module object to re use it's loader. – Tadhg McDonald-Jensen Apr 25 '16 at 13:27
-
`import` is used to import modules, `reload()` can also load certain parts of a module. – linusg Apr 25 '16 at 13:28
-
@cricket_007 if it was a statement it would just as easily be written `reload random #name` as appose to `reload(random) #module object` – Tadhg McDonald-Jensen Apr 25 '16 at 13:29
-
1I'd possibly check when the `reload` function was added. Since it's such a common name, I figure it would break a lot of existing code if it suddenly became a keyword. – Vincent Savard Apr 25 '16 at 13:34
-
Reolad function because call statement with changes ! import do nothing ! – dsgdfg Apr 25 '16 at 13:39
1 Answers
7
First of all you can import using a function, from importlib's documentation:
The
__import__()function
Theimportstatement is syntactic sugar for this function.
for instance both of these statements are equivalent:
from random import randint as random_int
random_int = __import__("random").randint
However the import statement greatly benefits from alternate syntax where as reload does not really have any alternate meaning.
I can also imagine a lot of beginner programmers making this mistake if reload was it's own statement:
from random import *
reload random #does not affect the current namespace!
Since the reload function requires a module (which is not preduced with from _ import *) coders may wonder why the names imported are not reloaded. related to this answer
Community
- 1
- 1
Tadhg McDonald-Jensen
- 19,220
- 4
- 32
- 57