On this page
collections.UserDict
Prev: collections.OrderedDict | Up: collections | Next: collections.UserList
Rel: collections.ChainMap, collections.Counter, collections.defaultdict, collections.deque
Ref: docs.python.org — collections.UserDict
Tags: #public #python3 #stdlib #collections
Import
import collections
from collections import UserDict
Signature & Overview
collections.UserDict(dict=None, /, **kwargs)
collections.UserDict class.
Methods & Attributes
UserDict.clear(self)— D.clear() -> None. Remove all items from D.UserDict.copy(self)—UserDict.copymethod/property.UserDict.fromkeys(iterable, value=None)—UserDict.fromkeysmethod/property.UserDict.get(self, key, default=None)—UserDict.getmethod/property.UserDict.items(self)— D.items() -> a set-like object providing a view on D's itemsUserDict.keys(self)— D.keys() -> a set-like object providing a view on D's keysUserDict.pop(self, key, default=<object object at 0x1040b41c0>)— value. If key is not found, d is returned if given, otherwise KeyError is raised.UserDict.popitem(self)— as a 2-tuple; but raise KeyError if D is empty.UserDict.setdefault(self, key, default=None)— D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in DUserDict.update(self, other=(), /, **kwds)— If E present and has a .keys() method, does: for k in E.keys(): D[k] = E[k] If E present and lacks .keys() method, does:UserDict.values(self)— D.values() -> an object providing a view on D's values
Usage
from collections import UserDict
class LowerKeyDict(UserDict):
def __setitem__(self, key: str, value) -> None:
super().__setitem__(key.lower(), value)
d = LowerKeyDict({"Host": "localhost"})
assert d["host"] == "localhost"
Gotchas
- Easier to subclass than built-in
dict: Built-indictmethods (like.update()or.setdefault()) bypass overridden__setitem__in C;UserDictroutes them through Python methods. - Access the backing dict via
.data:isinstance(d, dict)isFalse(checkcollections.abc.MutableMappinginstead).
REPL Inspect
from collections import UserDict
help(UserDict)
Linked from
Related notes
- tuts/python3/stdlib_module/collections/OrderedDict Linked here; links to this page; shared tags: #collections, #stdlib
- tuts/python3/stdlib_module/collections/UserList Linked here; links to this page; shared tags: #collections, #stdlib
- tuts/python3/stdlib_module/collections Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/collections/ChainMap Linked here; shared tags: #collections, #stdlib
- tuts/python3/stdlib_module/collections/Counter Linked here; shared tags: #collections, #stdlib
- tuts/python3/stdlib_module/collections/defaultdict Linked here; shared tags: #collections, #stdlib
- tuts/python3/stdlib_module/collections/deque Linked here; shared tags: #collections, #stdlib
- tuts/python3/stdlib_module/collections/namedtuple shared tags: #collections, #stdlib
- tuts/python3/stdlib_module/collections/UserString shared tags: #collections, #stdlib
- tuts/rust/std/collections shared tags: #collections, #stdlib