Hacktoberfest 2026:メンテナが10月に向けて印を付けた、オープンで初心者向けの issue。 Hacktoberfest の issue を見る

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

オープン 初心者向け
#159,034 コメント 5 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
76/100
issue の種類
ドキュメント
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
python
領域
documentation

調査の方向性

言語リファレンスの「6.10.2. Membership test operations」と「3.3.13. Special method lookup」を読み、メンバーシップテストの説明を「3.3.6.」の呼び出し可能オブジェクトの例と比較してください。暗黙的な特殊メソッドの検索を正確に反映するよう説明を更新し、issue内の例と周辺のリファレンス本文に照らして改訂後の表現を確認してください。

索引モデルが issue の本文から書いたものです。

説明

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
主要言語
Python
スター
77.6k
フォーク
37.6k
平均マージ
1日 16時間
マージ済み PR(30日)
612

環境構築

Codespaces で開く

このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

python/cpython のほかの issue

python/cpython の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。