Do more imported objects affect performance

Nick Craig-Wood nick at craig-wood.com
Tue Dec 2 13:30:47 EST 2008


Steven D'Aprano <steve at REMOVE-THIS-cybersource.com.au> wrote:
>  On Tue, 02 Dec 2008 11:12:31 +0000, Nick Craig-Wood wrote:
> 
> > I prefer the "from module import function".  That means that if "module"
> > doesn't supply "function" it raises an exception at compile time, not
> > run time when you try to run "module.function".
> 
>  Wanna bet?
> 
> 
> >>> def spam():
>  ...     from math import harmonic_series
>  ...     return harmonic_series()
>  ...
> >>> dis.dis(spam)
>    2           0 LOAD_CONST               1 (-1)
>                3 LOAD_CONST               2 (('harmonic_series',))
>                6 IMPORT_NAME              0 (math)
>                9 IMPORT_FROM              1 (harmonic_series)
>               12 STORE_FAST               0 (harmonic_series)
>               15 POP_TOP
> 
>    3          16 LOAD_FAST                0 (harmonic_series)
>               19 CALL_FUNCTION            0
>               22 RETURN_VALUE
> >>> spam()
>  Traceback (most recent call last):
>    File "<stdin>", line 1, in <module>
>    File "<stdin>", line 2, in spam
>  ImportError: cannot import name harmonic_series
> 
> 
>  The same thing happens if the from...import is at the top level of the 
>  module, except that compilation is immediately followed by
>  execution.

You are technically right I am sure.

However the error happens when you import the module with the error
in, not when you run stuff from it which is the major difference.

$ echo -e "from os import sausage\n" > import_test.py
$ python
Python 2.5.2 (r252:60911, Sep 29 2008, 21:15:13)
[GCC 4.3.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> # This does produce an error
>>> import import_test
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "import_test.py", line 1, in <module>
    from os import sausage
ImportError: cannot import name sausage
>>>
$ # This produces an error also
$ python import_test.py
Traceback (most recent call last):
  File "import_test.py", line 1, in <module>
    from os import sausage
ImportError: cannot import name sausage
$      

Unlike

$ echo -e "import os\ndef f(): os.sausage\n" > import_test.py
$ python
Python 2.5.2 (r252:60911, Sep 29 2008, 21:15:13)
[GCC 4.3.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> # This doesn't produce an error
>>> import import_test
>>> # Until you actually call it
>>> import_test.f()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "import_test.py", line 2, in f
    def f(): os.sausage
AttributeError: 'module' object has no attribute 'sausage'
>>>    
$ # No error here either
$ python import_test.py
$

> > It then becomes very easy to see which functions you use from any
> > given module too.
> 
>  If that's important to you. Personally, I find it more useful to know 
>  where a function is defined.

We can agree to differ there I'm sure ;-)

-- 
Nick Craig-Wood <nick at craig-wood.com> -- http://www.craig-wood.com/nick



More information about the Python-list mailing list