How do I make a defaultdict safe for unexpecting clients?

后端 未结 5 2149
情话喂你
情话喂你 2021-01-05 15:00

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

5条回答
  •  我在风中等你
    2021-01-05 15:03

    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".

提交回复
热议问题