Reputation: 10604
Say I have import a module by using
import m
and now I want an alias to its function, I can use
from m import f as n
or
n = m.f
I think there is no difference, is one preferred than another?
Upvotes: 4
Views: 143
Reputation: 6748
Use the first one if you only will need to get f. Use the second one if you need to import other things than f (m is still there). Other than that, they are the same.
Upvotes: 0
Reputation: 363063
There is no difference, as far as using the object n
is concerned.
There is a slight logical difference: the first way will leave a name m
bound in scope, and the second way will not. Though, the m
module would still get loaded into sys.modules
with either approach.
Using the import statement for this is more commonly seen.
Upvotes: 2