Skip to content

Document __instancecheck__ and __subclasscheck__ #49500

Closed

Description

@terryjreedy
BPO 5250
Nosy @birkenfeld, @terryjreedy
Files
  • datamodel.rst.diff: a draft
  • datamodel.rst.diff: trivial fixes
  • 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 = 'https://github.com/birkenfeld'
    closed_at = <Date 2010-04-14.21:35:00.741>
    created_at = <Date 2009-02-13.18:03:38.785>
    labels = ['type-feature', 'docs']
    title = 'Document __instancecheck__ and __subclasscheck__'
    updated_at = <Date 2010-04-19.21:03:25.694>
    user = 'https://github.com/terryjreedy'

    bugs.python.org fields:

    activity = <Date 2010-04-19.21:03:25.694>
    actor = 'cvrebert'
    assignee = 'georg.brandl'
    closed = True
    closed_date = <Date 2010-04-14.21:35:00.741>
    closer = 'georg.brandl'
    components = ['Documentation']
    creation = <Date 2009-02-13.18:03:38.785>
    creator = 'terry.reedy'
    dependencies = []
    files = ['16891', '16893']
    hgrepos = []
    issue_num = 5250
    keywords = ['patch']
    message_count = 6.0
    messages = ['81956', '102919', '103149', '103152', '103153', '103639']
    nosy_count = 3.0
    nosy_names = ['georg.brandl', 'terry.reedy', 'cvrebert']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = None
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue5250'
    versions = ['Python 2.6', 'Python 3.0', 'Python 3.1', 'Python 2.7', 'Python 3.2']

    Activity

    1. terryjreedy commented on Feb 13, 2009

      @terryjreedy
      MemberAuthor

      http://www.python.org/dev/peps/pep-3119/#overloading-isinstance-and-issubclass
      (GvR and Talin)
      "The primary mechanism proposed here is to allow overloading the
      built-in functions isinstance() and issubclass(). The overloading works
      as follows: The call isinstance(x, C) first checks whether
      C.__instancecheck__ exists, and if so, calls C.__instancecheck__(x)
      instead of its normal implementation. Similarly, the call issubclass(D,
      C) first checks whether C.__subclasscheck__ exists, and if so, calls
      C.__subclasscheck__(D) instead of its normal implementation."

      These two new special methods are not documented (for 3.x at least. A
      post on python-list today verifies that at least .__instancecheck__ was
      implemented (version not specified). I assume .__subclasscheck__ was
      also. This issue perhaps applies to 2.6/2.7 also.

      I suggest either adding "Customizing instance and subclass checks" after
      "Customizing class creation" or add to that section and rename it to
      "Customizing class creation and checks" since the needed addition seems
      too small for its own section. Something like

      The result of isinstance(object, class) can be modified by giving the
      *class* a .__instancecheck__(object) method. The result of
      issubclass(sub,super) can be modified by giving the *superclass* a
      .__subclasscheck__(sub) method.

    2. cvrebert commented on Apr 12, 2010

      cvrebertmannequin
      Mannequin

      Here's a draft of an updated version of the "Data model" docs reflecting the changes in the PEP.

    3. birkenfeld commented on Apr 14, 2010

      @birkenfeld
      Member

      Committed a similar patch in r80081. Thanks very much!

    4. cvrebert commented on Apr 14, 2010

      cvrebertmannequin
      Mannequin

      This might need to be reopened.

      There's a verb missing from he following sentence of the patch:
      "This is consistent with the lookup of special methods that called on instances, only that in this case the instance is itself a class."

      Secondly, and more importantly, the issubclass() and isinstance() docs don't mention the new special methods in question.

    5. birkenfeld commented on Apr 14, 2010

      @birkenfeld
      Member

      Fixed the missing verb in r80084. I don't think the new special methods need to be mention in isinstance() or issubclass() docs; they are very rarely needed, and other builtins don't mention their special methods either (e.g. abs(), complex(), or getattr().)

    6. cvrebert commented on Apr 19, 2010

      cvrebertmannequin
      Mannequin

      One last triviality: Could the mention of __subclasscheck__() in http://docs.python.org/dev/library/abc.html#abc.ABCMeta.\_\_subclasshook__ get linked to the newly-added docs?

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

    Labels

    docsDocumentation 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