Mutable default arguments are recreated on each function call instead of shared between calls
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 3/5
- Thời gian dự kiến
- 1-2 ngày
- Mức phù hợp với người mới
- 52/100
- Loại issue
- Lỗi
- Độ rõ ràng
- Khá rõ ràng
- Mức độ hoạt động
- Ít trao đổi
- Công nghệ
- javascript, python
- Lĩnh vực
- compilers
Hướng nghiên cứu
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.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
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
- Ngôn ngữ chính
- Python
- Star
- 2.9k
- Fork
- 218
- Chỉ số merge pull request
- Không có pull request nào được merge trong 30 ngày
Hướng dẫn đóng góp
Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của TranscryptOrg/Transcrypt
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 84/100
TranscryptOrg/Transcrypt#913 ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
TranscryptOrg/Transcrypt#911 · 2 bình luận ·
-
IS: bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
TranscryptOrg/Transcrypt#908 ·
-
SUB: documentation
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 62/100
TranscryptOrg/Transcrypt#656 · 7 bình luận ·
-
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 76/100
TranscryptOrg/Transcrypt#914 ·
Tất cả issue của TranscryptOrg/Transcrypt
Issue tương tự
-
triage/confirmed
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
agentscope-ai/agentscope#2775 ·
-
comp/desktop P3 type/bug
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 92/100
NousResearch/hermes-agent#118866 ·
-
bug
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 90/100
apache/cloudstack#14222 ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
-
bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100