Skip to content

dict init/update accesses internal items of dict derivative #45034

Closed

Description

@blakeross
mannequin
BPO 1730480
Nosy @rhettinger, @terryjreedy, @devdanzin

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 2010-08-09.04:02:30.774>
created_at = <Date 2007-06-04.03:59:53.000>
labels = ['interpreter-core', 'type-feature']
title = 'dict init/update accesses internal items of dict derivative'
updated_at = <Date 2010-08-11.20:24:29.695>
user = 'https://bugs.python.org/blakeross'

bugs.python.org fields:

activity = <Date 2010-08-11.20:24:29.695>
actor = 'rhettinger'
assignee = 'none'
closed = True
closed_date = <Date 2010-08-09.04:02:30.774>
closer = 'terry.reedy'
components = ['Interpreter Core']
creation = <Date 2007-06-04.03:59:53.000>
creator = 'blakeross'
dependencies = []
files = []
hgrepos = []
issue_num = 1730480
keywords = []
message_count = 4.0
messages = ['32207', '85623', '113372', '113627']
nosy_count = 4.0
nosy_names = ['rhettinger', 'terry.reedy', 'blakeross', 'ajaksu2']
pr_nums = []
priority = 'normal'
resolution = 'rejected'
stage = 'test needed'
status = 'closed'
superseder = None
type = 'enhancement'
url = 'https://bugs.python.org/issue1730480'
versions = ['Python 3.1', 'Python 2.7']

Activity

  1. blakeross commented on Jun 4, 2007

    blakerossmannequin
    MannequinAuthor
    >>> class MyDict(dict):
    ...     def keys(self): print "keys"
    ...     def __getitem__(self, n): print "__getitem__"
    ...
    >>> myDict = MyDict(a=1, b=2)
    >>> dict(myDict)
    {'a': 1, 'b': 2}

    PyDict_Merge accesses the items of the dict to be merged directly rather than going through the interface for any dict instance--even a dict derivative--by virtue of using PyDict_Check rather than PyDict_CheckExact. I believe the logic needs to be:

    if type(d).__getitem__ is dict.__getitem__ and type(d).keys is dict.keys:
        ...okay to access items directly...
    else:
        ...go through the methods...
  2. added
    interpreter-core
    on Jun 4, 2007
  3. devdanzin commented on Apr 6, 2009

    devdanzinmannequin
    Mannequin

    IIRC, the same behavior is present for subclasses of other builtin types.

  4. added
    type-feature
    on Apr 6, 2009
  5. terryjreedy commented on Aug 9, 2010

    @terryjreedy
    Member

    It is documented that access to special methods is more direct than normal path. This is not going to change. A similar example was discussed on python-list earlier this year with the above conclusion.

  6. rhettinger commented on Aug 11, 2010

    @rhettinger
    Contributor

    I concur with Terry.
    This is just a fact of life when subclassing builtins.
    The objects are "open-for-extension, closed-for-modification".
    If you want more direct control use UserDict.

  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

No one assigned

    Labels

    interpreter-coretype-feature

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions