Python random function

2019-02-09 03:59发布

I'm having problems with Python's import random function. It seems that import random and from random import random are importing different things. I am currently using Python 2.7.3

Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> random()

Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
    random()
NameError: name 'random' is not defined
>>> random.randint(1,5)

Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
random.randint(1,5)
NameError: name 'random' is not defined
>>> import random
>>> random()

Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
random()

TypeError: 'module' object is not callable
>>> random.randint(1,5)
2
>>> from random import random
>>> random()
0.28242411635200193
>>> random.randint(1,5)

Traceback (most recent call last):
File "<pyshell#7>", line 1, in <module>
random.randint(1,5)
AttributeError: 'builtin_function_or_method' object has no attribute 'randint'
>>> 

9条回答
甜甜的少女心
2楼-- · 2019-02-09 04:21

If you use from random import random, you must call randint() like so: randint(1,5). If you use import random, you call it like so: random.randint(1,5).

查看更多
混吃等死
3楼-- · 2019-02-09 04:26

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'.

查看更多
做自己的国王
4楼-- · 2019-02-09 04:28
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.

查看更多
不美不萌又怎样
5楼-- · 2019-02-09 04:31

Well, yes, they import different things. import random imports the random module, from random import random imports the random function from the random module. This is actually a good example of why when designing an API in Python, it's often a good idea to try to avoid naming modules and their members the same thing.

查看更多
成全新的幸福
6楼-- · 2019-02-09 04:32

The problem is that there are two things called random here: one is the module itself, and one is a function within that module. You can't have two things with the same name in your namespace so you have to pick one or the other.

查看更多
\"骚年 ilove
7楼-- · 2019-02-09 04:35

You have to import the random function, from the random module before you can use it use it

In [1]: from random import random

In [2]: random()
Out[2]: 0.5607917948041573
查看更多
登录 后发表回答