Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > why doesn't pop/clear call __delitem__ on a dict?

Reply
Thread Tools

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

 
 
Daniel Fetchinson
Guest
Posts: n/a
 
      12-11-2008
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
__delitem__ "feature" kj Python 5 12-28-2010 12:41 AM
why we should call Thread.start(),not directly call Thread.run()? junzhang1983@gmail.com Java 5 06-20-2008 03:12 PM
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
__delitem__ affecting performance Karl H. Python 1 10-19-2006 09:20 AM



Advertisments