What is the use of "from...import *" Statement in Python?


The "from module import *" statement is used to import all function from a Python module. For example, if you want to import all functions from math module and do not want to prefix "math." while calling them, you can do it as follows:

>>> from math import *
>>> sin(0)
0.0
>>> cos(0)
1.0

Note that for any reasonable large set of code, if you import * you will likely be cementing it into the module, unable to be removed. This is because it is difficult to determine what items used in the code are coming from 'module', making it easy to get to the point where you think you don't use the import any more but it's extremely difficult to be sure. It basically clutters the namespace and leaves you with lesser options to name things in your module.

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements