The from import statement
In Python, the from-import statement is used to selectively import specific names (variables, functions, classes, etc.) from a module into the current namespace.
The syntax of the from-import statement is:
javascript
from module_name import name1, name2, …
Here, module_name is the name of the module from which the names are to be imported, and name1, name2, etc. are the names of the specific items to be imported. If multiple names are to be imported, they are separated by commas.
For example, suppose we have a module named math_functions.py that contains several mathematical functions, including add, subtract, multiply, and divide. We can selectively import just the add and subtract functions into our main program using the from-import statement like this:
scss
from math_functions import add, subtract
result1 = add(2, 3)
result2 = subtract(5, 1)
Here, we import only the add and subtract functions from math_functions, which allows us to use them directly in our code without having to prefix them with the module name.
It’s worth noting that the from-import statement can make code easier to read and write in some cases, but it can also lead to namespace pollution and conflicts if used excessively or incorrectly. Therefore, it’s important to use it judiciously and consider the potential impacts on code maintainability and readability.
Apply for Python Certification!
https://www.vskills.in/certification/certified-python-developer