Special Method Protocols
__missing__() — containers protocol
Handle absent keys in dict subclass subscription. dict.__getitem__ invokes this hook; get(), membership and other dict methods do not automatically use it. The hook may return a default, insert a value or raise an error.
Formal Syntax & Structure
python syntaxpython
def __missing__(self, key):Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| key | object | Missing dictionary key. |
Return Value
object
Fallback value for missing key.
Working Example
Example Snippetpython
class Counts(dict): def __missing__(self,key): return 0print(Counts()["new"])