Hacker News new | past | comments | ask | show | jobs | submit login

Another option, it's very easy to wrap a dict in a defaultdict:

    class optionaldict(defaultdict):
	"""
	A defaultdict that disregards KeyErrors and returns None for missing keys.
	"""
	def __init__(self, *original, **kwargs):
	    super().__init__(lambda: None, *original, **kwargs)



This is almost always a bad idea, though; it doesn't distinguish between

    {k: None}[k]
and

    {}[k]


It's not a generic option, you are polluting memory with non-existing keys.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: