Mutable default arguments are recreated on each function call instead of shared between calls
还没有人认领这个 Issue。
评估
- 难度
- 3/5
- 预计耗时
- 1-2 天
- 新手友好度
- 52/100
- Issue 类型
- 缺陷
- 描述清晰度
- 基本清楚
- 活跃度
- 冷清
- 技术栈
- javascript, python
- 领域
- compilers
调研方向
Start with the minimal Python example in the issue and compare its Transcrypt-generated JavaScript with CPython's output. Trace how omitted default arguments are emitted, then verify that repeated calls share the mutable default and that the identity check reports True.
由索引模型根据 Issue 内容生成。
描述
Description
Mutable default arguments appear to behave differently in Transcrypt compared with CPython.
In Python, default argument expressions are evaluated when the function definition is executed. Therefore, when a mutable object such as [] is used as a default argument, calls that omit the argument use the same object.
In Transcrypt 3.9.5, the default list appears to be recreated for every function call instead.
Minimal example
Tested with Python 3.9 and Transcrypt 3.9.5:
def append_list1(new_item, lst=[]):
lst.append(new_item)
return lst
print(append_list1(1), append_list1(2))
def append_list2(new_item, lst=[]):
lst.append(new_item)
return lst
lst1 = append_list2(1)
lst2 = append_list2(2)
print(lst1 is lst2)
CPython output
[1, 2] [1, 2]
True
Output when running the Transcrypt-generated JavaScript
[1] [2]
False
Expected behaviour
The default value of lst should be created once when the function is defined. Therefore, calls to append_list2() without explicitly supplying lst should receive the same list object, and lst1 is lst2 should be True.
Generated JavaScript
The generated JavaScript appears to initialise the default argument inside the function when the argument is omitted, conceptually equivalent to:
var append_list2 = function (new_item, lst) {
if (typeof lst == 'undefined') {
var lst = [];
}
lst.append(new_item);
return lst;
};
This causes a new list to be created each time the function is called without lst, rather than once when the function is defined.
Environment
Python: 3.9.0
Transcrypt: 3.9.5
Node.js: v24.19.0
OS: Windows 11
- 主要语言
- Python
- 星标
- 2.9k
- 派生
- 217
- PR 合并指标
- 30 天内没有已合并 PR
环境准备
这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
TranscryptOrg/Transcrypt 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 84/100
TranscryptOrg/Transcrypt#913 ·
-
难度 2/5 1-3 小时 新手友好度 88/100
TranscryptOrg/Transcrypt#911 · 2 条评论 ·
-
IS: bug
难度 2/5 1-3 小时 新手友好度 70/100
TranscryptOrg/Transcrypt#908 ·
-
Update installation and testing instructions可能已有人在做 关联的 PR 仍在进行中或已合并。 未关闭SUB: documentation
难度 1/5 1 小时以内 新手友好度 62/100
TranscryptOrg/Transcrypt#656 · 7 条评论 ·
-
re: character classes are parsed as regular expressions可能已有人在做 @CarstenGrohmann 于 41 天前认领。 未关闭
难度 3/5 1-2 天 新手友好度 76/100
TranscryptOrg/Transcrypt#914 ·
查看 TranscryptOrg/Transcrypt 的全部 Issue
相似的 Issue
-
area:space-accuracy good first issue track:data
难度 2/5 1-3 小时 新手友好度 85/100
Sara-Managed-Projects/space-radar#904 ·
维护者通常 1 天内回复