python - Most Performant Way To Do Imports -
from performance point of view (time or memory) better do:
import pandas pd
or
from pandas import dataframe, timeseries
does best thing depend on how many classes i'm importing package?
similarly, i've seen people things like:
def foo(bar): numpy import array
why ever want import inside function or method definition? wouldn't mean import being performed every time function called? or avoid namespace collisions?
this micro-optimising, , should not worry this.
modules loaded once per python process. code imports need bind name module or objects defined in module. binding extremely cheap.
moreover, top-level code in your module runs once too, binding takes place once. import in function binding each time function run, again, cheap negligible.
importing in function makes difference 2 reasons: won't put name in global namespace module (so no namespace pollution), , because name local, using name faster using global.
if want improve performance, focus on code being repeated many, many times. importing not it.
Comments
Post a Comment