+ 1
Is inserting a module in Python as 'import math' is same as writing the statement, 'from math import *' ? ?
Actually, I'm little bit confused.
3 ответов
+ 2
actually, the whole module is imported, even if you do from math import pi...
Thus, there is no difference in performance when importing the whole module or only what you need.
However, when you do from math import *, all the variables and functions have to be bound in the main namespace, thus it can be a bit slower.
If you really need the whole math module, do import math and you can access pi by doing math.pi (for instance)
+ 3
In the example you provided they are the same. The star means ALL. import math says import all of the math module. The other one says from math import all.. So you see they are the same. There are some cases where this would be a little different. But the main thing to remember is that you don't want to import more than you need, it will only slow the program down. So if all you need is the pi function, for example, then do from math import pi. Don't import all of math. For best results just look up the proper way to import each module you want to use.
+ 3
Correction: My professor told us that importing all of a module would be more processor intensive. After researching on my own, I found that Amaras is right, it doesn't make much difference. However, importing only what you need appears to be the common approach. The reasons are simply to have less typing in the code and to avoid using variables that are reserved by the modules. It creates less confusion.
Now I'm upset. Gonna need to talk to my prof, lol.