Is it possible to design a dictionary in Python in a way that if by mistake a key which is already in the dictionary is added, it gets rejected? thanks
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
You can always create your own dictionary
Just check your dict before you add the item
This is the purpose of setdefault:
This also means you can skip "if 'key' in dict: ... else: ..."
You could create a custom dictionary by deriving from
dict
and overriding__setitem__
to reject items already in the dictionary.