Mutable default arguments are recreated on each function call instead of shared between calls
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 3/5
- Tempo stimato
- 1-2 giorni
- Idoneità per principianti
- 52/100
- Tipo di issue
- Bug
- Chiarezza
- Abbastanza chiara
- Stato di attività
- Tranquilla
- Stack tecnologico
- javascript, python
- Ambito
- compilers
Direzione di ricerca
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.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
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
- Lingua principale
- Python
- Stelle
- 2.9k
- Fork
- 218
- Metriche di merge delle PR
- Nessuna PR unita negli ultimi 30g
Guida per i contributori
Nessuna guida per i contributori indicizzata per questo repository
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di TranscryptOrg/Transcrypt
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 84/100
TranscryptOrg/Transcrypt#913 ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 88/100
TranscryptOrg/Transcrypt#911 · 2 commenti ·
-
IS: bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 70/100
TranscryptOrg/Transcrypt#908 ·
-
SUB: documentation
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 62/100
TranscryptOrg/Transcrypt#656 · 7 commenti ·
-
Difficoltà 3/5 1-2 giorni Idoneità per principianti 76/100
TranscryptOrg/Transcrypt#914 ·
Tutte le issue di TranscryptOrg/Transcrypt
Issue simili
-
area: harness bug status: needs-triage
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
Human-Agent-Society/reef#625 ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 70/100
-
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 80/100
learningequality/kolibri#15351 · 2 commenti ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
-
Name consistency Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
eellak/triplestore#65 · 1 commento ·