3 ms·
Why is import a Python keyword?
- dalke 13y ago> As far as I can tell, the main argument against an import function is aesthetic. Compare: "from random import bar" to "bar = __import__('random').bar" I agree. A stronger pro-aesthetic example might be: >>> from xml.sax.saxutils import escape -vs- >>> escape = __import__("xml.sax.saxutils").sax.saxutils.escape As another aesthetic, this is much more complicated with __import__: >>> from itertools import repeat as _repeat, chain as _chain, starmap as _starmap and requires an intermediate variable to reduce import resolution overhead. (That example is from itertools/__init__.py . The ctypes/__init__.py also has a similarly complicated import-as .)
- Rangi42 13y agoFor the common cases of "import foo" and "from foo import bar", having a keyword avoids repeating the module/function name, as in "foo = import('foo')" and "bar = import('foo').bar". Ignoring this detail, "import" could be simply a function, like "print" and "exec" in Python 3. And with lazy evaluation, even statements could be made into function calls: "if(True, 42, 1/0)" would evaluate to 42 without causing a ZeroDivisionError. The same can be done for assignment: "locals().__setitem__('x', 3)" instead of "x = 3". Keep this up and programs might start to look like this: define('fac', ('x',), product(range(1, x+1))) Which is basically a copy of Lisp: (defun fac (x) (product (range 1 (+ 1 x))))
- arxanas 13y agoOne thing which isn't immediately handled is `from foo import *`. There's not really any way to put all of the symbols in `foo` into the current scope with the function syntax.