AttributeError: 'builtin_function_or_method' object has no attribute 'sample'
时间: 2023-08-27 18:16:55 浏览: 291
【Python】AttributeError: ‘AutoSchema’ object has no attribute ‘get_link’
5星 · 资源好评率100%
这个报错的原因是你导入的是random模块中的random方法,而不是整个random模块。所以当你尝试调用random模块中的其他方法时,会出现'builtin_function_or_method' object has no attribute 'sample'这个错误。要解决这个问题,你可以使用import random来导入整个random模块,而不是只导入random方法。这样你就可以调用random模块中的任何方法,包括sample方法。例如: import random print(random.sample([1, 2, 3, 4, 5], 3))<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *3* [Python使用中报错信息:AttributeError: ‘builtin_function_or_method‘ object has no attribute ...](https://blog.csdn.net/lanxingbudui/article/details/124505489)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
- *2* [第一章 Python初探](https://blog.csdn.net/qq_22895113/article/details/123479907)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文