apple apple
apple apple

Reputation: 10604

create module function alias by import or assignment

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

Answers (2)

whackamadoodle3000
whackamadoodle3000

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

wim
wim

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

Related Questions