Skip to main content
 首页 » 编程设计

python之datetime.timedelta(x,y) 在 CoCalc.com 上返回 TypeError 但在其他地方有效——为什么

2025年05月04日13jillzhang

我的代码在 onlinegdb.com 上有效,但在 CoCalc.com 上无效。

import datetime 
slowduration = datetime.timedelta(0,1) 
print(slowduration) 

返回

TypeError: unsupported type for timedelta seconds component: sage.rings.integer.Integer 

我不清楚这是功能还是错误。

请您参考如下方法:

为了补充@kcrisman 的回答和“int(0), int(1)”技巧...

如果想坚持使用 Sage 内核,还有另外两个选择

  • (1) 使用preparser(False) 禁用预解析器,
  • (2) 将 r(表示“原始”)附加到整数,例如 datetime.timedelta(0r, 1r)。

另请参阅有关 Sage 的 float 和整数预解析的类似问题和答案:

最后,请注意,可以使用以下任一方式从外部文件将代码加载到 Sage 中:

  • load('/path/to/filename.py')
  • load('/path/to/filename.sage')

.sage 文件将获得“Sage-prepared”,而 .py 文件则不会。

这提供了绕过预解析器的第三种选择:从 .py 文件加载代码。