Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

Mutable default arguments are recreated on each function call instead of shared between calls

未关闭
#909 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 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 内容生成。

描述

IS: bug?
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,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

TranscryptOrg/Transcrypt 的其他 Issue

查看 TranscryptOrg/Transcrypt 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。