How to import a single function from a Python module?

PythonServer Side ProgrammingProgramming

You can use the "from module import function" statement to import a specific function from a Python module. For example, if you want to import the sin function from the math library without importing any other function, you can do it as follows:

>>> from math import sin
>>> sin(0)
0.0

Note that you don't have to prefix sin with "math." as only sin has been imported and not math. Also you can alias imported functions. For example,

>>> from math import cos as cosine
>>> cosine(0)
1.0
raja
Published on 19-Dec-2017 16:25:51
Advertisements