How do I make a defaultdict safe for unexpecting clients?

馋奶兔 提交于 2019-12-19 03:16:13

问题


Several times (even several in a row) I've been bitten by the defaultdict bug: forgetting that something is actually a defaultdict and treating it like a regular dictionary.

d = defaultdict(list)

...

try:
  v = d["key"]
except KeyError:
  print "Sorry, no dice!"

For those who have been bitten too, the problem is evident: when d has no key 'key', the v = d["key"] magically creates an empty list and assigns it to both d["key"] and v instead of raising an exception. Which can be quite a pain to track down if d comes from some module whose details one doesn't remember very well.

I'm looking for a way to take the sting out of this bug. For me, the best solution would be to somehow disable a defaultdict's magic before returning it to the client.


回答1:


You can prevent creation of default values by assigning d.default_factory = None. However, I don't quite like the idea of object suddenly changing behavior. I'd prefer copying values to the new dict unless it imposes severe performance penalty.




回答2:


You may still convert it to an normal dict.

d = collections.defaultdict(list)
d = dict(d)



回答3:


use different idiom:

if 'key' not in d:
    print "Sorry, no dice!"



回答4:


That is exactly the behavior you want from a defaultdict and not a bug. If you dont't want it, dont use a defaultdict.

If you keep forgetting what type variables have, then name them appropriately - for example suffix your defaultdict names with "_ddict".




回答5:


Using rkhayrov's idea of resetting self.default_factory, here is a toggleable subclass of defaultdict:

class ToggleableDefaultdict(collections.defaultdict):
    def __init__(self,default_factory):
        self._default_factory=default_factory
        super(ToggleableDefaultdict,self).__init__(default_factory)
    def off(self):
        self.default_factory=None
    def on(self):
        self.default_factory=self._default_factory

For example:

d=ToggleableDefaultdict(list)
d['key'].append(1)
print(d)
# defaultdict(<type 'list'>, {'key': [1]})

d.off()
d['newkey'].append(2)
# KeyError: 'newkey'

d.on()
d['newkey'].append(2)
# defaultdict(<type 'list'>, {'newkey': [2], 'key': [1]})


来源:https://stackoverflow.com/questions/3031817/how-do-i-make-a-defaultdict-safe-for-unexpecting-clients

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!