Let me create a module with two functions and Add Multiply
I call this module as prog1.py
Prog1.py
consists of the following code:
# prog1.py def add(x,y,z): return (x+y+z) def multiply(x,y): return (x * y) Copy
Let me create another module with two functions and the names the functions are: Add and Divide.
I call this module as prog2.py
# prog2.py def add(x,y): return (x+y) def divide(x,y): return (x / y)Copy
In another program prog3.py
, I import these two modules as follows:
#prog3.py import prog1 import prog2 a = prog1.add(10,20,30) print("addition from prog1",a) b = prog2.add(10,20) print("addition from prog2", b)Copy
This type of importing statement would remove the ambiguity. It works fine. Both the function calls would be executed and we get the results.
#prog4.py from prog1 import add from prog2 import add a = add(10,20,30) print("addition from prog1",a) b = add(10,20) print("addition from prog2", b)Copy
Here the interpreter would consider the latest one i.e,
from prog2 import add
The above program would throw the following error:
Traceback (most recent call last): File "prog4.py", line 4, in a = add(10,20,30) TypeError: add() takes 2 positional arguments but 3 were givenCopy
#prog5.py from prog2 import add from prog1 import add b = add(10,20) print("addition from prog2", b) a = add(10,20,30) print("addition from prog1",a) Traceback (most recent call last): File "prog5.py", line 7, in b = add(10,20) TypeError: add() missing 1 required positional argument: 'z'Copy
Here the above program is considering the latest import which is from prog1 import add
We have to be careful if two modules has same function with different parameters while importing.