Message380882
When tinkering around with `SimpleNamespace` I tried to figure out the reasons for using it over just the `class NS: pass` alternative.
One reason is the comparison, which is not a plain `id` comparison, but an attribute comparison.
When looking at the documentation of the imaginary python implementation, where only dicts are compared, the reader (me) could think you can compare it to classes.
https://docs.python.org/3/library/types.html?highlight=simplenamespace#types.SimpleNamespace
```
>>> from types import SimpleNamespace
>>> simple_ns = SimpleNamespace(a=1, b="two")
>>> class NS: pass
>>> class_ns = NS()
>>> class_ns.a = 1
>>> class_ns.b = "two"
>>> simple_ns == class_ns
>>> False
```
Actually, the C implementation compares the types, too.
https://github.com/python/cpython/blob/bc777047833256bc6b10b2c7b46cce9e9e6f956c/Objects/namespaceobject.c#L163-L171
While the documentation says the Python impl is "roughly equivalent",
I's still suggest to update the documentation.
If there is some agreement, I'd create a pull request. |
|
Date |
User |
Action |
Args |
2020-11-13 13:42:06 | jugmac00 | set | recipients:
+ jugmac00 |
2020-11-13 13:42:06 | jugmac00 | set | messageid: <1605274926.75.0.147847269171.issue42344@roundup.psfhosted.org> |
2020-11-13 13:42:06 | jugmac00 | link | issue42344 messages |
2020-11-13 13:42:06 | jugmac00 | create | |
|