FROM random IMPORT *和IMPORT random之间有什么区别? (random()和randrange())

时间:2022-06-25 02:55:40

In the book has been this codesample:

在书中一直是这个代码示例:

from random import*for i in range(15):                             print random.randrange(3,13,3)

And it have got result in the book.

它已经在书中得到了结果。

But when I run it in the Netbeans. The following excaption arosed:

但是当我在Netbeans中运行它时。出现以下情况:

*

Traceback (most recent call last): File "C:\Users\Lacces\Documents\NetBeansProjects\Python_GS_Tanuljunk_meg_programozni\src\Adatszerkezetek\Lista.py", line 11, in print random.randrange(3,13,3) #3-tól 13-ig, 3 érték elválasztásal AttributeError: 'builtin_function_or_method' object has no attribute 'randrange'

回溯(最近一次调用最后一次):文件“C:\ Users \ Lacces \ Documents \ NetBeansProjects \ Python_GS_Tanuljunk_meg_programozni \ src \ Adatszerkezetek \ Lista.py”,第11行,在print random.randrange(3,13,3)#3- tól13-ig,3értékelválasztásalNameError:'builtin_function_or_method'对象没有属性'randrange'

*

I have call to help the google, and I found this for the import:

我打电话来帮助谷歌,我发现这是为了导入:

import random

With that I used this instead of from random import*

有了这个我使用它而不是随机导入*

And it worked! No exception!

它奏效了!没有例外!

Can somebody explain to me why throw exception at the first time, and why not at the second time (for a beginner programmer:))

有人可以向我解释为什么第一次抛出异常,为什么不在第二次(对于初学者程序员:))

4 个解决方案

#1


7  

When you from random import *, all the definitions from random become part of the current name space. This means you don't have to prefix anything with random., but it also means you may get a name collision without even knowing it.

当您从随机导入*时,随机的所有定义都将成为当前名称空间的一部分。这意味着您不必使用随机的前缀。,但这也意味着您可能会在不知情的情况下获得名称冲突。

The preferred way is import random.

首选方法是随机导入。

#2


3  

Importing everything from a module is discouraged just because of these surprising side effects: The module random contains a function random, so import * from random does the following:

由于这些令人惊讶的副作用,不鼓励从模块导入所有内容:模块随机包含一个随机函数,因此import * from random执行以下操作:

from random import randrangefrom random import random...

Now, when you're accessing random, you're accessing the function instead of the module. You could use randrange (without the prefix random.), but import random and explicitly stating what module a function is from is the better idea.

现在,当您随机访问时,您正在访问函数而不是模块。你可以使用randrange(没有前缀随机。),但导入随机并明确说明函数来自哪个模块是更好的主意。

#3


2  

If you use 'from moduleName import ....' then you get all classes, functions and variables that you specified after import or all if you specify *.: from random import * for i in range(15):
print randrange(3,13,3)

如果您使用'from moduleName import ....',那么您将获得导入后指定的所有类,函数和变量,如果您指定*:from from import import * for i in range(15):print randrange(3) ,13,3)

But note that this is not very good to import all, it would be better to import only required classes, functions and variables so in case you need only randrange you need to use:

但请注意,导入all并不是很好,最好只导入必需的类,函数和变量,这样只需要你需要使用randrange:

from random import randrangefor i in range(15):                         print randrange(3,13,3)

In case you are using import random this means that you importing module so you need to specify moduleName. when you want to use it so:

如果您使用import random,这意味着您导入模块,因此您需要指定moduleName。当你想要使用它时:

import randomfor i in range(15):                         print random.randrange(3,13,3)

#4


2  

from random import * imports all functions from a module called random, but not random itself.

来自随机导入*从一个名为random的模块中导入所有函数,但不是随机的。

Here you can directly call the functions in random as follows: randrange(3,13,3), etc

在这里你可以随机直接调用函数,如下所示:randrange(3,13,3)等

import random imports the name random, from which you can later call functions as follows: random.randrange(3,13,3), etc

import random随机导入名称,以后可以调用函数,如下所示:random.randrange(3,13,3)等

#1


7  

When you from random import *, all the definitions from random become part of the current name space. This means you don't have to prefix anything with random., but it also means you may get a name collision without even knowing it.

当您从随机导入*时,随机的所有定义都将成为当前名称空间的一部分。这意味着您不必使用随机的前缀。,但这也意味着您可能会在不知情的情况下获得名称冲突。

The preferred way is import random.

首选方法是随机导入。

#2


3  

Importing everything from a module is discouraged just because of these surprising side effects: The module random contains a function random, so import * from random does the following:

由于这些令人惊讶的副作用,不鼓励从模块导入所有内容:模块随机包含一个随机函数,因此import * from random执行以下操作:

from random import randrangefrom random import random...

Now, when you're accessing random, you're accessing the function instead of the module. You could use randrange (without the prefix random.), but import random and explicitly stating what module a function is from is the better idea.

现在,当您随机访问时,您正在访问函数而不是模块。你可以使用randrange(没有前缀随机。),但导入随机并明确说明函数来自哪个模块是更好的主意。

#3


2  

If you use 'from moduleName import ....' then you get all classes, functions and variables that you specified after import or all if you specify *.: from random import * for i in range(15):
print randrange(3,13,3)

如果您使用'from moduleName import ....',那么您将获得导入后指定的所有类,函数和变量,如果您指定*:from from import import * for i in range(15):print randrange(3) ,13,3)

But note that this is not very good to import all, it would be better to import only required classes, functions and variables so in case you need only randrange you need to use:

但请注意,导入all并不是很好,最好只导入必需的类,函数和变量,这样只需要你需要使用randrange:

from random import randrangefor i in range(15):                         print randrange(3,13,3)

In case you are using import random this means that you importing module so you need to specify moduleName. when you want to use it so:

如果您使用import random,这意味着您导入模块,因此您需要指定moduleName。当你想要使用它时:

import randomfor i in range(15):                         print random.randrange(3,13,3)

#4


2  

from random import * imports all functions from a module called random, but not random itself.

来自随机导入*从一个名为random的模块中导入所有函数,但不是随机的。

Here you can directly call the functions in random as follows: randrange(3,13,3), etc

在这里你可以随机直接调用函数,如下所示:randrange(3,13,3)等

import random imports the name random, from which you can later call functions as follows: random.randrange(3,13,3), etc

import random随机导入名称,以后可以调用函数,如下所示:random.randrange(3,13,3)等