python / python/cpython

Clarify which variables appear in function.__closure__

Aperta
#142,093 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

docs
Lingua principale
Python
Stelle
77.2k
Fork
35.9k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

While working with nested functions, I observed that function.__closure__ only
contains cells for variables that are actually referenced inside the inner
function body. Variables defined in the enclosing scope but not used in the
inner function do not appear in co_freevars and therefore do not appear in
function.__closure__.

Example:

def outer():
    x = 1
    y = 2
    z = 3
    def inner():
        return x
    return inner

inner.__closure__  # contains only x

This behavior matches the execution model (“free variables”), but the current
documentation only states that __closure__ contains bindings for names listed
in co_freevars. It does not explicitly state that only referenced names are
included.

This PR proposes adding a brief clarification to make this behavior explicit.

Before continuing with the pull request, I would like confirmation from the
documentation team whether this detail should be included in the data model
section.

Linked PRs
  • gh-142086

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia dalla documentazione del modello dati di Python e rivedi la descrizione esistente di function.closure insieme a co_freevars. Chiarisci che compaiono solo i nomi a cui fa riferimento la funzione interna e conferma la formulazione con il team della documentazione prima di procedere; il lavoro è completato quando il comportamento è esplicito nella sezione pertinente.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
documentation
Tipo di issue
Documentazione
Difficoltà
2/5
Tempo stimato
1-3 ore
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.