python - 最大递归深度超过了写一个简单的函数
问题描述
def AtomicWeight(Z: "int"):
return AtomicWeight(Z)
z = 1.45
AtomicWeight(z)
type(z)
有人可以解释这个函数在做什么以及为什么它说超出了最大递归深度吗?我尝试增加最大递归,但它不起作用。有没有另一种写法?
解决方案
该函数一遍又一遍地调用自己,因为return
函数中没有。在调用函数 x 次后,超出了最大递归深度,Python 停止运行代码。
这可以通过将您的示例更改为:
def AtomicWeight(Z: "int"):
return Z * 2 # times 2 is just example to let the function do something
z = 1.45
z = AtomicWeight(z)
现在该AtomicWeight
函数不再调用自身并返回一个值。
推荐阅读
- rust - 如何使用参数实现 trait 默认实现
- java - 使用 Java 重新连接到进程
- static-libraries - Mingw64:添加静态链接库添加 libstdc++ 依赖项
- python - 如何最好地存储和调用在单个 XLSX 文件上运行的 pandas 脚本?
- python - python中属性装饰器对可变属性的用处
- node.js - 如何使用 Jest 库测试基本的 socket.io Node.js 应用程序?
- discord.js - 不和谐机器人有可能进入国外服务器吗?
- javascript - 为什么 React Context 只会触发从 DOM 元素向下传递的值函数
- apache-kafka - 用于实时交易平台的 kafka 每分钟处理数百万美元的交易?
- python - 如何在 if else 语句之后继续你的代码而不中断?