What's the different between the two?
import random
and
from random import random
What's the different between the two?
import random
and
from random import random
import random
imports the entire module in its own namespace, so you must access all its methods via
random.some_method_name()
so to access its random method, you need to say:
number = random.random()
The method random.random() is just one of many methods in this module. If you only need a subset of the methods in a module, you can use;
from random import method_1, method_2, ......
These methods appear in the current namespace and are referenced as method_1(), method_1() etc.
so
from random import random
provides you just with module's random method and is accessed without needing the module name as in
number = random()
That's was a great reply. Thank you.
Excercising my dusty Professor circuits. Your welcome.
Great answer for me also.