Python random function

前端 未结 9 1147
后悔当初
后悔当初 2020-12-10 03:26

I\'m having problems with Python\'s import random function. It seems that import random and from random import random are importing different thing

9条回答
  •  孤街浪徒
    2020-12-10 04:06

    The 'random' module is a package from the python standard library, as well as a function defined in this package.

    Using 'import random' imports the package, which you can then use the function from this package: 'random.random()'. You can use any other function from the 'random' package as well.

    You can also tell python to specifically import only the random function from the package random: 'from random import random'. Then you can only use the function 'random()', and should not specify the package it comes from. However you cannot use any other function from the random package, because they have not been imported if you use 'from random import random'.

提交回复
热议问题