python / python/cpython

Clarify which variables appear in function.__closure__

Offen
#142,093 1 Kommentar 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

docs
Vorherrschende Sprache
Python
Sterne
77.2k
Forks
35.9k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

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

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne in der Python-Dokumentation zum Datenmodell und überprüfe die bestehende Beschreibung von function.closure zusammen mit co_freevars. Stelle klar, dass nur von der inneren Funktion referenzierte Namen enthalten sind, und bestätige die Formulierung mit dem Dokumentationsteam, bevor du fortfährst; abgeschlossen bedeutet, dass das Verhalten im relevanten Abschnitt eindeutig beschrieben ist.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
documentation
Issue-Typ
Dokumentation
Schwierigkeit
2/5
Geschätzter Aufwand
1-3 Stunden
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.