'builtin_function_or_method' 对象没有属性 'randrange'

新手上路,请多包涵

我有一个奇怪的问题,

我创建了以下代码来随机生成 1 和 x 之间的数字,增量为 1 并存储它们

import random

bootstrap_node_list_recieved = [] #List of all nodes addresses recieved during the bootstrap peroid - Is a list so we can compare duplicatition probability etc
average_getAdrr_no_node_response = 100 #Number or nodes typically sent when a node requests a getAddr message
network_ip_node_size = 5000 # Number of IP addresses / nodes that have been seen on the network in the past 2 weeks

#Move into calculations.py when ready
#Number of nodes recieved (Bootstrap)
def bootstrap_node_getAddr():
    #### TODO ####
    #Random generation of nodes (number represents a single node), from 1 to x for an average amount of nodes
    # node_list=[random.randrange(1,network_ip_node_size,1) for _ in range (average_getAdrr_no_node_response)]
    for i in range (average_getAdrr_no_node_response):
        bootstrap_node_list_recieved.append(random.randrange(1,network_ip_node_size,1))
    print 'bootstrap_node_getAddr: ', bootstrap_node_list_recieved
    # return bootstrap_node_list_recieved

bootstrap_node_getAddr()

此代码本身运行良好,但是当我将其插入我的主代码库时出现错误

Traceback (most recent call last):
  File "BootstrapBTC.py", line 117, in query_dns_servers
    bootstrap_node_getAddr()
  File "/home/richard/Dropbox/PhD/Simulator BTC - Ours/Calculations.py", line 33, in bootstrap_node_getAddr
    bootstrap_node_list_recieved.append(random.randrange(1,network_ip_node_size,1))
AttributeError: 'builtin_function_or_method' object has no attribute 'randrange'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "BootstrapBTC.py", line 90, in run
    yield self.env.process(query_dns_servers(env, self))
AttributeError: 'builtin_function_or_method' object has no attribute 'randrange'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "BootstrapBTC.py", line 178, in <module>
    env.run()
  File "/usr/local/lib/python2.7/dist-packages/simpy/core.py", line 137, in run
    self.step()
  File "/usr/local/lib/python2.7/dist-packages/simpy/core.py", line 229, in step
    raise exc
AttributeError: 'builtin_function_or_method' object has no attribute 'randrange

第 90 行的代码是

    yield self.env.process(query_dns_servers(env, self))

这只是调用

#Average respsonse time from a DNS server
def DnsServerResponse(env, self):
    yield self.env.timeout(dns_average_response)

它在模拟时间中附加了一个随机值,我认为它与这行代码没有任何关系,因为它根本不使用随机库,事实上插入的代码是迄今为止唯一使用该库的代码

任何人都知道问题是什么?这让我发疯!

谢谢 !

尝试将我的导入更改为随机导入,现在得到:

 Traceback (most recent call last):
  File "BootstrapBTC.py", line 102, in query_dns_servers
    DnsUp = DnsUpProbability()
  File "/home/richard/Dropbox/PhD/Simulator BTC - Ours/Calculations.py", line 39, in DnsUpProbability
    up = (0 if random() > Prob_DNS_UP else 1)
TypeError: 'module' object is not callable

原文由 user4017041 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 888
2 个回答

该错误是因为您很可能将 random 分配给代码中某处的另一个值,以覆盖随机模块的原始 random

尝试检查您的代码,看看您还随机分配了什么。或者你可以通过这样做来检查它:在你使用随机之前

print(random, type(random))

解决此问题的另一种方法是,如果您的代码太长且难以检查,您可以像这样随机导入:使用 import…as… 格式。

 import random as rand
# and when using it, type rand instead of random
bootstrap_node_list_recieved.append(rand.randrange(1,network_ip_node_size,1))

您还应该检查您的 sys.path 中是否有任何名为 random.py 的文件。

原文由 Taku 发布,翻译遵循 CC BY-SA 3.0 许可协议

任何人都知道问题是什么?这让我发疯!

此错误的通常原因是有人将文件命名为“random.py”并且正在读取该文件而不是标准库中的文件。

对此进行测试的一种简单方法是查看 随机 来自何处:

 print(random.__file__)

另一个可能的原因是 随机import random 之后被分配了一些其他值。尝试检查:

 print(type(random))         # This should be a module object.

也试试:

 help(random)                # To see what this object is.

这是一个示例,它会准确给出您所看到的错误消息:

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

在这种情况下,解决方案是将 from random import random 转换为 import random

原文由 Raymond Hettinger 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题