why doesn't pop/clear call __delitem__ on a dict?

Discussion in 'Python' started by Daniel Fetchinson, Dec 11, 2008.

  1. I just found out that if I want to have a custom dict it's not enough
    to overload __getitem__, __setitem__ and __delitem__ because, for
    example, pop and clear don't call __delitem__. I.e. an instance of the
    following will not print 'deleted' upon instance.pop( 'key' ):

    class mydict( dict ):
    def __setitem__( self, key, value ):
    print 'set'
    super( mydict, self ).__setitem__( key, value )
    def __getitem__( self, key ):
    print 'get'
    super( mydict, self ).__getitem__( key )
    def __delitem__( self, key ):
    print 'deleted'
    super( mydict, self ).__delitem__( key )

    Why is this? There might other gotchas too I suppose. My intention is
    clear from the above, what other methods do I have to overload so that
    I get what I expect for all dict operations?

    Cheers,
    Daniel

    --
    Psss, psss, put it down! - http://www.cafepress.com/putitdown
     
    Daniel Fetchinson, Dec 11, 2008
    #1
    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. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,135
    Smokey Grindel
    Dec 2, 2006
  2. Karl H.

    __delitem__ affecting performance

    Karl H., Oct 19, 2006, in forum: Python
    Replies:
    1
    Views:
    238
    Fredrik Lundh
    Oct 19, 2006
  3. David

    Response.Clear() doesn't clear

    David, Jan 31, 2008, in forum: ASP .Net
    Replies:
    2
    Views:
    1,093
    Mark Fitzpatrick
    Jan 31, 2008
  4. kj

    __delitem__ "feature"

    kj, Dec 26, 2010, in forum: Python
    Replies:
    5
    Views:
    216
    Ian Kelly
    Dec 28, 2010
  5. samppi
    Replies:
    27
    Views:
    530
    David A. Black
    Dec 5, 2007
Loading...

Share This Page