__delitem__ "feature"

Discussion in 'Python' started by kj, Dec 26, 2010.

  1. kj

    kj Guest

    When I execute this file:

    #----------------------------------------------------------------------
    def nodelfactory(klass):
    class nodel(klass):
    def _delitem(self, _):
    raise TypeError("can't delete")

    # __delitem__ = _delitem

    def __init__(self, *a, **k):
    klass.__init__(self, *a, **k)
    self.__delitem__ = self._delitem

    nodel.__name__ = 'nodel%s' % klass.__name__
    return nodel


    if __name__ == '__main__':
    import traceback as tb


    d = nodelfactory(dict)([('k1', 'v1'), ('k2', 'v2')])

    try: d.__delitem__('k1')
    except TypeError: tb.print_exc()
    print d

    try: del d['k1']
    except TypeError: tb.print_exc()
    print d


    l = nodelfactory(list)([1, 2, 3, 4])

    try: l.__delitem__(0)
    except TypeError: tb.print_exc()
    print l

    try: del l[0]
    except TypeError: tb.print_exc()
    print l
    #----------------------------------------------------------------------

    ....the output I get is:

    Traceback (most recent call last):
    File "/tmp/delbug.py", line 20, in <module>
    try: d.__delitem__('k1')
    File "/tmp/delbug.py", line 4, in _delitem
    raise TypeError("can't delete")
    TypeError: can't delete
    {'k2': 'v2', 'k1': 'v1'}
    {'k2': 'v2'}
    Traceback (most recent call last):
    File "/tmp/delbug.py", line 30, in <module>
    try: l.__delitem__(0)
    File "/tmp/delbug.py", line 4, in _delitem
    raise TypeError("can't delete")
    TypeError: can't delete
    [1, 2, 3, 4]
    [2, 3, 4]

    It means that, for both subclasses, del fails to trigger the
    dynamically installed instance method __delitem__.

    If I replace dict with UserDict, *both* deletion attempts lead to
    a call to the dynamic __delitem__ method, and are thus blocked.
    This is the behavior I expected of dict (and will help me hold on
    to my belief that I'm not going insane when inevitably I'm told
    that there's no bug in dict or list).

    Interestingly enough, if I replace list with UserList, I see no
    change in behavior. So maybe I am going insane after all.

    ~kj

    P.S. If you uncomment the commented-out line, and comment out the
    last line of the __init__ method (which installs self._delitem as
    self.__delitem__) then *all* the deletion attempts invoke the
    __delitem__ method, and are therefore blocked. FWIW.
     
    kj, Dec 26, 2010
    #1
    1. Advertising

  2. kj

    Ian Kelly Guest

    On 12/26/2010 10:53 AM, kj wrote:
    > P.S. If you uncomment the commented-out line, and comment out the
    > last line of the __init__ method (which installs self._delitem as
    > self.__delitem__) then *all* the deletion attempts invoke the
    > __delitem__ method, and are therefore blocked. FWIW.


    Because subclasses of builtins only check the class __dict__ for special
    method overrides, not the instance __dict__.
     
    Ian Kelly, Dec 26, 2010
    #2
    1. Advertising

  3. kj

    kj Guest

    In <> Ian Kelly <> writes:

    >On 12/26/2010 10:53 AM, kj wrote:
    >> P.S. If you uncomment the commented-out line, and comment out the
    >> last line of the __init__ method (which installs self._delitem as
    >> self.__delitem__) then *all* the deletion attempts invoke the
    >> __delitem__ method, and are therefore blocked. FWIW.


    >Because subclasses of builtins only check the class __dict__ for special
    >method overrides, not the instance __dict__.



    How do you know this? Is this documented? Or is this a case of
    Monday-night quarterbacking?

    ~kj
     
    kj, Dec 26, 2010
    #3
  4. On Sun, 26 Dec 2010 18:49:55 +0000, kj wrote:

    > In <> Ian Kelly
    > <> writes:
    >
    >>On 12/26/2010 10:53 AM, kj wrote:
    >>> P.S. If you uncomment the commented-out line, and comment out the last
    >>> line of the __init__ method (which installs self._delitem as
    >>> self.__delitem__) then *all* the deletion attempts invoke the
    >>> __delitem__ method, and are therefore blocked. FWIW.

    >
    >>Because subclasses of builtins only check the class __dict__ for special
    >>method overrides, not the instance __dict__.

    >
    >
    > How do you know this? Is this documented? Or is this a case of
    > Monday-night quarterbacking?


    We know it because it explains the observable facts.

    It also happens to be documented, but documentation can be wrong or
    incomplete. The facts are never wrong, since by definition they are the
    facts.



    --
    Steven
     
    Steven D'Aprano, Dec 27, 2010
    #4
  5. kj

    kj Guest

    In <4d181afb$0$30001$c3e8da3$> Steven D'Aprano <> writes:

    >We know it because it explains the observable facts.


    So does Monday-night quarterbacking...
     
    kj, Dec 27, 2010
    #5
  6. kj

    Ian Kelly Guest

    On 12/26/2010 11:49 AM, kj wrote:
    > In<> Ian Kelly<> writes:
    >
    >> On 12/26/2010 10:53 AM, kj wrote:
    >>> P.S. If you uncomment the commented-out line, and comment out the
    >>> last line of the __init__ method (which installs self._delitem as
    >>> self.__delitem__) then *all* the deletion attempts invoke the
    >>> __delitem__ method, and are therefore blocked. FWIW.

    >
    >> Because subclasses of builtins only check the class __dict__ for special
    >> method overrides, not the instance __dict__.

    >
    >
    > How do you know this?


    From memory, although it seems I remembered it slightly wrong; it's the
    way new-style classes work in general, not anything to do with builtins
    in particular.

    > Is this documented?


    Yes, as others have pointed out.

    > Or is this a case of Monday-night quarterbacking?


    Do you mean "Monday-morning quarterbacking"? Either way, I don't know
    what you mean by that in this context.
     
    Ian Kelly, Dec 28, 2010
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Midnight Java Junkie

    Office Development (feature addition)

    Midnight Java Junkie, Jun 20, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    470
    Midnight Java Junkie
    Jun 20, 2004
  2. =?Utf-8?B?U2FuamVldiBCaGF0aWE=?=

    Regarding Browse information(ALT-F12) feature same as VS 6.0

    =?Utf-8?B?U2FuamVldiBCaGF0aWE=?=, Apr 6, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    890
    =?Utf-8?B?U2FuamVldiBCaGF0aWE=?=
    Apr 8, 2005
  3. Replies:
    0
    Views:
    525
  4. Karl H.

    __delitem__ affecting performance

    Karl H., Oct 19, 2006, in forum: Python
    Replies:
    1
    Views:
    238
    Fredrik Lundh
    Oct 19, 2006
  5. Daniel Fetchinson
    Replies:
    0
    Views:
    246
    Daniel Fetchinson
    Dec 11, 2008
Loading...

Share This Page