Forum Archive

import question

donnieh

What's the different between the two?

import random

and

from random import random
polymerchm

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()

donnieh

That's was a great reply. Thank you.

polymerchm

Excercising my dusty Professor circuits. Your welcome.

Phuket2

Great answer for me also.