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

D

Daniel Fetchinson

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top