A minor problem with the section "6.10.2. Membership test operations" from the language reference.
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
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 yreturnsTrueify.__contains__(x)returns a true value, andFalseotherwise.
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 totype(x).__call__(x, arg1, ...).
Turning our problematic expression into
For user-defined classes which define the
__contains__()method,x in yreturnsTrueif the the call to__contains__()method (which roughly translates totype(y).__contains__(y, x)) returnsTrue, otherwiseFalse.
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
Starts the project's dev container in your browser, under your own GitHub account.
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from python/cpython
-
stdlib type-bug
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
python/cpython#159044 · 4 comments ·
Maintainers usually reply within 1 day
-
Docs: socket.htons() and friends raise ValueError for negative input since 3.14Possibly taken A pull request linked to this issue is open or already merged. Opendocs
Difficulty 1/5 1-3 hours Newbie friendliness 78/100
python/cpython#159012 · 1 comment ·
Maintainers usually reply within 1 day
-
stdlib topic-profiling type-bug
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Maintainers usually reply within 1 day
-
stdlib type-bug
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Maintainers usually reply within 1 day
-
lazy import of `_sysconfig` in `Lib/ctypes/__init__.py` is eager on android and cygwinPossibly taken @sillyfellow21 claimed this 5 days ago. Opentests topic-lazy-imports
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
python/cpython#158684 · 4 comments ·
Maintainers usually reply within 1 day
Similar issues
-
Difficulty 1/5 Under an hour Newbie friendliness 85/100
MystenLabs/MemWal#1163 · 2 comments ·
Maintainers usually reply within 1 day
-
infertopics leaves new nodes without a topic when untopiced neighbours outnumber topiced onesPossibly taken @moneebullah25 claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
ClanGenOfficial/clangen#6254 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
FinanceFlash/unvibecode#218 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
Maintainers usually reply within 1 day