Hacktoberfest 2026: những issue maintainer đã đánh dấu cho tháng Mười, đang mở và phù hợp người mới. Xem issue Hacktoberfest

A minor problem with the section "6.10.2. Membership test operations" from the language reference.

Đang mở Phù hợp với người mới
#159,034 5 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

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

Đánh giá

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

Hướng nghiên cứu

Đọc các mục tham chiếu ngôn ngữ “6.10.2. Membership test operations” và “3.3.13. Special method lookup”, so sánh cách diễn đạt về phép kiểm tra thành viên với ví dụ đối tượng có thể gọi trong “3.3.6.” Cập nhật phần mô tả để phản ánh chính xác việc tra cứu phương thức đặc biệt ngầm định, sau đó đối chiếu cách diễn đạt đã sửa với các ví dụ trong issue và phần văn bản tham chiếu xung quanh.

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

Mô tả

docs pending
Documentation

The section "6.10.2. Membership test operations" of the language reference contains the following assertion:

For user-defined classes which define the __contains__() method, x in y returns True if y.__contains__(x) returns a true value, and False otherwise.

But it's false; this code:

class _:
    def __contains__(self, y):
        return True
y = _()
y.__contains__ = lambda y: False
x=1
print(y.__contains__(x))
print(x in y)

Will yield

False
True

To be precise, the current description ignores the special method lookup described in section "3.3.13. Special method lookup". Simply changing it to type(y).__contains__(y, x) will not really solve the issue either, as the last example from section 3.3.13 shows:

class Meta(type):
    def __getattribute__(*args):
        print("Metaclass getattribute invoked")
        return type.__getattribute__(*args)

class C(object, metaclass=Meta):
    def __len__(self):
        return 10
    def __getattribute__(*args):
        print("Class getattribute invoked")
        return object.__getattribute__(*args)

c = C()
c.__len__()                 # Explicit lookup via instance
Class getattribute invoked
10
type(c).__len__(c)          # Explicit lookup via type
Metaclass getattribute invoked
10
len(c)                      # Implicit lookup
10

The straightforward fix I can think of is to use the same way of describing it as, say, the "3.3.6. Emulating callable objects" section with:

object.call(self[, args...])
Called when the instance is “called” as a function; if this method is defined, x(arg1, arg2, ...) roughly translates to type(x).__call__(x, arg1, ...).

Turning our problematic expression into

For user-defined classes which define the __contains__() method, x in y returns True if the the call to __contains__() method (which roughly translates to type(y).__contains__(y, x)) returns True, otherwise False.

Note that the problem is present from at least Python 3.0 onwards.

Linked PRs
  • gh-159046
Ngôn ngữ chính
Python
Star
77.6k
Fork
37.6k
Merge trung bình
1 ngày 20 giờ
Pull request đã merge (30 ngày)
608

Chuẩn bị môi trường

Mở trong Codespaces

Khởi chạy dev container của dự án ngay trên trình duyệt, bằng tài khoản GitHub của bạn.

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.

Issue khác của python/cpython

Tất cả issue của python/cpython

Issue tương tự

Thêm issue về Python

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.