Hacktoberfest 2026: the issues maintainers tagged for October, open and beginner-friendly. Browse Hacktoberfest issues

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

Open Beginner friendly
#159,034 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
76/100
Issue type
Documentation
Clarity
Clearly specified
Activity status
Active
Tech stack
python
Domain
documentation

Research direction

Read the language reference sections “6.10.2. Membership test operations” and “3.3.13. Special method lookup,” comparing the membership-test wording with the callable-object example in “3.3.6.” Update the description to reflect implicit special-method lookup accurately, then verify the revised wording against the examples in the issue and the surrounding reference text.

Written by the indexing model from the issue text.

Description

docs
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.

Dominant language
Python
Stars
77.6k
Forks
37.6k
Avg merge
1d 10h
Merged PRs (30d)
573

Getting set up

Open in Codespaces

Starts the project's dev container in your browser, under your own GitHub account.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from python/cpython

All issues in python/cpython

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.