简体   繁体   中英

What does the interpreter do, when executing “from xyz import Abc”?

What does the interpreter do in these cases?

  1. from xyz import Abc
  2. from xyz import *

Does it has to parse the full file xyz.py anyway? Should one way be faster then the other? Or is it merely a matter of readability that people prefer the first approach to the second?

It should not make any difference performance-wise, because the entire module has to be processed either way.

The difference is one of readability, and pollution of namespaces. To minimize the chances of name-clashes, and unexpected behavior due to name-hiding, it is prudent to only import those objects that you are actually using.

from Martijn Pieters :

All top-level names you import into a module, can also be imported again from that module unless a __all__ parameter has been set. Thus you usually end up with far more items being added to your local namespace than you bargained for

difference in namespace after each command:

>>> from math import *
>>> len(vars())
46
>>> vars()
{'pow': <built-in function pow>, 'fsum': <built-in function fsum>, 'cosh': <built-in function cosh>, 'ldexp': <built-in function ldexp>, 'hypot': <built-in function hypot>, 'acosh': <built-in function acosh>, 'tan': <built-in function tan>, 'asin': <built-in function asin>, 'isnan': <built-in function isnan>, 'log': <built-in function log>, 'fabs': <built-in function fabs>, 'floor': <built-in function floor>, 'atanh': <built-in function atanh>, 'sqrt': <built-in function sqrt>, '__package__': None, 'frexp': <built-in function frexp>, 'factorial': <built-in function factorial>, 'degrees': <built-in function degrees>, 'pi': 3.141592653589793, 'log10': <built-in function log10>, '__doc__': None, 'asinh': <built-in function asinh>, 'fmod': <built-in function fmod>, 'atan': <built-in function atan>, '__builtins__': <module '__builtin__' (built-in)>, 'copysign': <built-in function copysign>, 'cos': <built-in function cos>, 'ceil': <built-in function ceil>, 'atan2': <built-in function atan2>, 'isinf': <built-in function isinf>, 'sinh': <built-in function sinh>, '__name__': '__main__', 'trunc': <built-in function trunc>, 'expm1': <built-in function expm1>, 'e': 2.718281828459045, 'tanh': <built-in function tanh>, 'radians': <built-in function radians>, 'sin': <built-in function sin>, 'lgamma': <built-in function lgamma>, 'erf': <built-in function erf>, 'erfc': <built-in function erfc>, 'modf': <built-in function modf>, 'exp': <built-in function exp>, 'acos': <built-in function acos>, 'log1p': <built-in function log1p>, 'gamma': <built-in function gamma>}


>>> ================================ RESTART ================================


>>> from math import log10
>>> len(vars())
5
>>> vars()
{'__builtins__': <module '__builtin__' (built-in)>, '__package__': None, '__name__': '__main__', 'log10': <built-in function log10>, '__doc__': None}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM