python / python/cpython

Clarify which variables appear in function.__closure__

Đang mở
#142,093 1 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

docs
Ngôn ngữ chính
Python
Star
77.2k
Fork
35.9k
Chỉ số merge pull request
Chỉ số pull request đang chờ

Mô tả

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

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. 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.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu trong tài liệu mô hình dữ liệu Python và xem lại phần mô tả hiện có về function.closure cùng với co_freevars. Làm rõ rằng chỉ những tên được hàm bên trong tham chiếu mới xuất hiện, đồng thời xác nhận cách diễn đạt với nhóm tài liệu trước khi tiếp tục; được coi là hoàn tất khi hành vi được nêu rõ trong phần liên quan.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
python
Lĩnh vực
documentation
Loại issue
Tài liệu
Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
35/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.