* imports all objects from a module like from math import *. But import math also imports all objects right? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

* imports all objects from a module like from math import *. But import math also imports all objects right?

5th Jun 2017, 7:04 PM
Soumyadeep Mondal
Soumyadeep Mondal - avatar
2 Answers
+ 12
# The difference is that with 'from' you can use those methods directly, without the module name. # 1. import math print(math.sqrt(9)) # 2. from math import * print(sqrt(9)) # 3. import math as m print(m.sqrt(9)) # 4. from math import sqrt as s print(s(9))
5th Jun 2017, 7:24 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
0
I would add that the "from module import *" is not recommended (see PEP 8). It is usually better to simply use "import module".
9th Jun 2017, 9:44 AM
Bogdan Sass
Bogdan Sass - avatar