Importing general knowledge question


I have begun studying python ( first look at coding ever, other than an confused unsuccessful short stint with BASIC on a 128K spectrum when I was 6)

I have reached the importing from modules section and it shows the following

import math
from math import *

I was just wondering with the * method not requiring .math before every action from the math module, why would anyone ever use "import math" rather than the * method of importing?


Oh that's an easy one to answer, considering the fact, that it is ALREADY answered later in the tutorial (here be dragons). Now, why not explaining it here too, right? So, the "import math" method, imports every function as math.something(). That's cool, but "from math import *" imports everything as something() and no need to type math. ever again! Cool right?... Well, no. In more complex programs, the simple "something()" name of the function, can end up being a duplicate of another function that you made, or you imported from another library. Remember! Most of the times, you won't import just one module, and sometimes you might import many modules for the same thing, thus, resulting into having multiple functions having the same name. Now that happens only if you imported *. But if you do it casually, like "import module", now every function has the module in front of it like "module.function()" and duplicates are harder to encounter.

Sorry for long reply, i was just a newbie at the whole programming thing a year ago, I am just retaking the tutorial to get better... Hope I helped


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.