python / python/cpython

Need more info regarding cells and closures.

Đang mở
#98,176 1 bình luận 1 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
36k
Chỉ số merge pull request
Chỉ số pull request đang chờ

Mô tả

Cell variable

Since "free variable" is defined in 4.2.1. Binding of names, I suggest also defining a "cell variable", as any local variable whose name is resolved to that scope in some enclosed scope.
Also, the term "free variable" is used elsewhere in the docs, where it means that the variable is resolved in an enclosing function scope, and does not include if the variable is resolved in the module scope.

exec(..., closure=...)

The closure argument specifies a closure–a tuple of cellvars. It’s only valid when the object is a code object containing free variables. The length of the tuple must exactly match the number of free variables referenced by the code object.

  1. Use the name 'func' to refer to the function involved. func.__code__ is the object given to exec().
  2. Instead of 'cellvars', say 'bindings for free variables in 'func'.' Refer to new 'Cell objects' section of the Type Hierarchy.
  3. The bindings are in the same order as the names of the free variables found in code.co_freevars.
  4. The bindings can be found in func.__closure.
  5. func.__closure__ reflects values of the free variables at the time exec is called, which may have changed since func was created.
  6. Bindings in closure can be modified by the code if the variable names are declared nonlocal in func. If 'closure' is func.__closure__, then the 'func' free variables will have changed.
  7. Refer to new 'Cell Objects' section in the Type Hierarchy for details.

Type Hierarchy

Add a section for 'Cell Objects' under 'Internal types'.

  • The name of the class is 'cell', and the class is available in types.CellType.
  • It represents the binding of a free or cell variable.
  • It may have a current value (any Python object), or it may be empty.
  • cell() is an empty cell. cell(obj) is a cell with obj as its current value.
  • cell.cell_contents is a writable attribute returning the current value, if any, or raising ValueError if empty. It may be assigned a new value or deleted (without raising an exception if already empty).
  • Cells may be compared by their values. Empty cells compare < non-empty cells.

Mention that Frame objects have class 'frame', which is types.FrameType. There is no explicit constructor.

User-defined functions section:

  • Add an id=user-defined-functions tag to the HTML so that it can be linked to as a fragment.
  • Under closure, refer to the new 'Cell objects' section, instead of the later paragraph about cells.

inspect.getmembers

Add entry for 'closure' under 'function'. Use same text as above in the user-defined functions section.

Entry for 'co_cellvars' under 'code', should read 'contained scopes' instead of 'containing scopes'. And it is only for names in 'co_varnames'.

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 với các phần tài liệu được liên kết về exec(closure=...), Type Hierarchy, các hàm do người dùng định nghĩa và inspect.getmembers. Kiểm tra các định nghĩa hiện có về biến tự do, biến ô, closure và frame trước khi cập nhật thuật ngữ và các tham chiếu chéo được yêu cầu; công việc được hoàn thành khi các phần bị ảnh hưởng mô tả nhất quán hành vi ô và closure được yêu cầu.

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ó
4/5
Thời gian dự kiến
3-5 ngày
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
45/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.