Python random function

前端 未结 9 1135
后悔当初
后悔当初 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:22

    import random 
    

    includes the module into the namespace under the name 'random'.

    from random import random
    

    includes the function'random' from the namespace 'random' into the global namespace.

    So in the first example, you would call random.random, and in the second, you would call random. Both would access the same function.

    Similarly,

    from random import randint
    

    would import randint into the global namespace, so you could simply call randint instead of random.randint.

提交回复
热议问题