Do more imported objects affect performance
Steven D'Aprano
steve at REMOVE-THIS-cybersource.com.au
Tue Dec 2 08:47:14 EST 2008
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.
> 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.
--
Steven
More information about the Python-list
mailing list