Skip to content

Improve documentation for list methods that compare items by equality #73942

Description

@atodorov
mannequin
BPO 29756
Nosy @warsaw, @stevendaprano, @MojoVampire, @zhangyangyu, @atodorov
PRs
  • bpo-29756: Improve documentation for list methods that compare items by equality #572
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2017-03-13.02:11:02.276>
    created_at = <Date 2017-03-08.14:03:53.249>
    labels = ['type-feature', '3.7', 'docs']
    title = 'Improve documentation for list methods that compare items by equality'
    updated_at = <Date 2017-03-24.22:20:58.976>
    user = 'https://github.com/atodorov'

    bugs.python.org fields:

    activity = <Date 2017-03-24.22:20:58.976>
    actor = 'xiang.zhang'
    assignee = 'docs@python'
    closed = True
    closed_date = <Date 2017-03-13.02:11:02.276>
    closer = 'xiang.zhang'
    components = ['Documentation']
    creation = <Date 2017-03-08.14:03:53.249>
    creator = 'Alexander Todorov'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 29756
    keywords = []
    message_count = 9.0
    messages = ['289235', '289236', '289239', '289240', '289243', '289270', '289512', '289519', '290190']
    nosy_count = 6.0
    nosy_names = ['barry', 'steven.daprano', 'docs@python', 'josh.r', 'xiang.zhang', 'Alexander Todorov']
    pr_nums = ['572']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue29756'
    versions = ['Python 3.7']

    Activity

    1. atodorov commented on Mar 8, 2017

      atodorovmannequin
      MannequinAuthor

      When using list.count() I get the following results

          >>> [1, 2, 3].count(1)
          1
          >>> [1, 2, 3, True].count(2)
          1
          >>> [1, 2, 3, True].count(True)
          2
          >>> [1, 2, 3, True].count(1)
          2

      as you can see True is considered the same as 1. The documentation for the count method says:

          count(...)
              L.count(value) -> integer -- return number of occurrences of value

      so IMO the above behavior is wrong. Seeing this on a RHEL 7 system with
      Python 3.5.1 and 2.7.5

    2. warsaw commented on Mar 8, 2017

      @warsaw
      Member

      bools are subclasses of int and False and True have integer equivalents:

      https://docs.python.org/3/library/stdtypes.html#bltin-boolean-values

    3. stevendaprano commented on Mar 8, 2017

      @stevendaprano
      Member

      Further to Barry's explanation, you see the same result with any values which compare equal:

      py> from decimal import Decimal as D
      py> [1, 1.0, D(1), True, 1+0j].count(D(1))
      5

      This is standard behaviour for methods count, remove, and index, but it isn't explained well in the documentation. E.g. remove says "Remove the first item from the list whose value is x` which could be read as meaning that the test is done by identity. All three methods need to clarify that ordinary == equality is used.

      I'm going to re-open the task as a documentation issue.

    4. changed the title [-]List count() counts True as 1[/-] [+]Improve documentation for list methods that compare items by equality[/+] on Mar 8, 2017
    5. stevendaprano commented on Mar 8, 2017

      @stevendaprano
      Member

      To be clear, I'm referring to the docs in the tutorial:

      https://docs.python.org/3.7/tutorial/datastructures.html

      and the docstrings as well as the library reference:

      https://docs.python.org/3.7/library/stdtypes.html#sequence-types-list-tuple-range

      The library reference already notes that remove uses equality, but the others do not.

    6. MojoVampire commented on Mar 8, 2017

      MojoVampiremannequin
      Mannequin

      Steven: Technically, in CPython, they use both identity and equality testing, as a function of using RichCompareBool (which tests identity first, then equality), rather than RichCompare (which only tests equality).

      It makes a difference for stuff like NaN values, where describing it as equality only would imply that:

      nan = float('nan')
      ([nan] * 10).count(nan)

      produces 0 (because nan is equal to nothing, including itself), when in fact it produces 10 (because we reused the same nan object, and the identity test passed).

    7. zhangyangyu commented on Mar 9, 2017

      @zhangyangyu
      Member

      +1 for jsoh. Actually the behaviour is documented in https://docs.python.org/3/reference/expressions.html#value-comparisons.

      So a single 'a is b' or 'a == b' is not complete. I would like to use 'equal to' which implies 'a is b or a == b'.

    8. atodorov commented on Mar 12, 2017

      atodorovmannequin
      MannequinAuthor

      Hi folks,
      I have another very similar issue, it could be the same root cause. Let me know if it is.

      assert 3 == 3.0 will pass

      self.assertEqual(3, 3.0) - will pass

      this had the nasty side effect of my test suite not catching a problem with SUT. I have updated the test suite to validate the result type as well but want to know how to file this use-case.

    9. stevendaprano commented on Mar 13, 2017

      @stevendaprano
      Member

      I'm afraid I don't know what SUT means.

      But 3 == 3.0 is the correct and expected behaviour. If you need to
      check that two values are both the same type and the same value, you
      have to validate the type and value separately.

      Changing the behaviour of == is ruled out for backwards
      compatability, but perhaps you could suggest a new === operator
      to check type and value. That would require some discussion on
      the Python Ideas mailing list, not just a feature request on the
      tracker.

    10. zhangyangyu commented on Mar 24, 2017

      @zhangyangyu
      Member

      New changeset b2d7717 by Xiang Zhang in branch 'master':
      bpo-29756: Improve documentation for list methods that compare items by equality (GH-572)
      b2d7717

    11. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      3.7 (EOL)end of lifedocsDocumentation in the Doc dirtype-featureA feature request or enhancement

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions