classification
Title: SimpleNamespace: update documentation regarding comparison
Type: Stage: resolved
Components: Versions:
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: eric.smith, jugmac00, miss-islington
Priority: normal Keywords: patch

Created on 2020-11-13 13:42 by jugmac00, last changed 2020-11-13 18:19 by eric.smith. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 23264 merged jugmac00, 2020-11-13 15:21
PR 23269 merged miss-islington, 2020-11-13 18:17
PR 23270 merged miss-islington, 2020-11-13 18:17
Messages (6)
msg380882 - (view) Author: Jürgen Gmach (jugmac00) * Date: 2020-11-13 13:42
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.
msg380885 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2020-11-13 13:55
If the implementation compares the classes, then I think the "roughly equivalent" version should, too.
msg380897 - (view) Author: Jürgen Gmach (jugmac00) * Date: 2020-11-13 15:21
Thanks for your feedback. I created a PR on github.
msg380914 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2020-11-13 18:15
New changeset bbeb2d266d6fc1ca9778726d0397d9d6f7a946e3 by Jürgen Gmach in branch 'master':
bpo-42344: Improve pseudo implementation for SimpleNamespace (GH-23264)
https://github.com/python/cpython/commit/bbeb2d266d6fc1ca9778726d0397d9d6f7a946e3
msg380915 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2020-11-13 18:18
New changeset cb2b2035ca752529755440990c4073d5164e80df by Miss Islington (bot) in branch '3.8':
bpo-42344: Improve pseudo implementation for SimpleNamespace (GH-23264) (GH-23269)
https://github.com/python/cpython/commit/cb2b2035ca752529755440990c4073d5164e80df
msg380916 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2020-11-13 18:19
New changeset 4defeb007195d2d17ea404b0b6291d1d233010f4 by Miss Islington (bot) in branch '3.9':
bpo-42344: Improve pseudo implementation for SimpleNamespace (GH-23264) (GH-23270)
https://github.com/python/cpython/commit/4defeb007195d2d17ea404b0b6291d1d233010f4
History
Date User Action Args
2020-11-13 18:19:08eric.smithsetmessages: + msg380916
2020-11-13 18:18:39eric.smithsetmessages: + msg380915
2020-11-13 18:17:42miss-islingtonsetpull_requests: + pull_request22166
2020-11-13 18:17:33miss-islingtonsetnosy: + miss-islington

pull_requests: + pull_request22165
2020-11-13 18:16:41eric.smithsetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2020-11-13 18:15:45eric.smithsetmessages: + msg380914
2020-11-13 15:21:57jugmac00setkeywords: + patch

stage: patch review
messages: + msg380897
pull_requests: + pull_request22161
2020-11-13 13:55:12eric.smithsetnosy: + eric.smith
messages: + msg380885
2020-11-13 13:42:06jugmac00create