Open
Description
Feature or enhancement
Proposal:
SimpleNamespace is a useful lightweight object container, but currently:
from types import SimpleNamespace
ns = SimpleNamespace(a=1, b=2)
len(ns) # Raises TypeError
Adding a len() method would improve its usability by allowing:
len(ns) # returns 2
This matches user expectations since its attributes are stored in dict.
Proposed Implementation:
Define:
def __len__(self):
return len(self.__dict__)
This is simple, backwards-compatible, and testable.
Benefits:
Improves discoverability
Makes SimpleNamespace feel more like a container
Zero risk of breakage
I would like to implement this as a small contribution.
Has this already been discussed elsewhere?
This is a minor feature, which does not need previous discussion elsewhere
Links to previous discussion of this feature:
No response