Skip to content Skip to sidebar Skip to footer

On Import Modules And Method Names In Python

Suppose I import the following two modules as follows: from sympy import * from numpy import * both modules have an exp() function defined. How does python pick which one to use?

Solution 1:

It doesn't "pick". When you do from sympy import * it imports all names from sympy into the current namespace; and when you do from numpy import * it does the same thing. Anything that is previously defined is overwritten. This is exactly the same as if you did:

foo = 'bar'foo = 'baz'

Clearly, foo now has the value "baz" even though you initially defined it as "bar".

The solution is not to do this; you should import the things you need explicitly:

from sympy import exp, ....
from numpy import ....

Solution 2:

As far as I know and according to your code, the numpy's exp() would be added to your namespace as it's the last module which was imported. So, in this case, the last one wins the race! To fix this just use :

from toys import yo-yo as yo-yo1

Post a Comment for "On Import Modules And Method Names In Python"