Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OrderedDict dict/list representation #101446

Closed
SimpleArt opened this issue Jan 31, 2023 · 2 comments
Closed

OrderedDict dict/list representation #101446

SimpleArt opened this issue Jan 31, 2023 · 2 comments
Labels
type-feature A feature request or enhancement

Comments

@SimpleArt
Copy link

SimpleArt commented Jan 31, 2023

I know that for historical purposes, OrderedDict.__repr__ displays key-value pairs in the format of [(key, value), ...], since the {key: value, ...} representation did not respect the ordering that OrderedDict needs. However, this was only in the past and it has become a while since dictionaries have become ordered, so I was wondering if changing the OrderedDict.__repr__ to display OrderedDict({key: value, ...}) instead would be fine. The main rationale is that this is syntactically cleaner to read, often I have found the other representation to be a lot more clunky. Other than potentially breaking things like doc-tests, I don't see much issue with this. The only potential ambiguity that I see is the fact that one might not be immediately sure that the dictionary is ordered, but the fact that it is wrapped by OrderedDict(...) would probably dispel any doubts.

Linked PRs

@arhadthedev arhadthedev added the type-feature A feature request or enhancement label Feb 2, 2023
@rhettinger
Copy link
Contributor

I agree that the proposed new __repr__ would be an improvement.

sobolevn added a commit to sobolevn/cpython that referenced this issue Feb 7, 2023
rwgk pushed a commit to rwgk/pybind11 that referenced this issue Mar 9, 2023
…aks between Python 3.12alpha3 (still working) and 3.12alpha6 (broken):

```
str(OrderedDict([(1, "a"), (2, "b")]))
```

Old:
```
OrderedDict([(1, 'a'), (2, 'b')])
```

New:
```
OrderedDict({1: 'a', 2: 'b'})
```

See also: python/cpython#101446
rwgk pushed a commit to pybind/pybind11 that referenced this issue Mar 9, 2023
…aks between Python 3.12alpha3 (still working) and 3.12alpha6 (broken): (#4559)

```
str(OrderedDict([(1, "a"), (2, "b")]))
```

Old:
```
OrderedDict([(1, 'a'), (2, 'b')])
```

New:
```
OrderedDict({1: 'a', 2: 'b'})
```

See also: python/cpython#101446
@rwgk
Copy link

rwgk commented Mar 9, 2023

Currently this change is mentioned only in the 3.12 Changelog but not under the top-level What’s New In Python 3.12 page.

@Yhg1s suggested that it should be mentioned in What’s New, too. Should I file a separate bug for this, or is this comment sufficient as a reminder?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

4 participants