This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Inspect - Added sort_result parameter on getmembers function.
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.11
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: Patitotective, serhiy.storchaka
Priority: normal Keywords:

Created on 2021-09-25 14:28 by Patitotective, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 28547 closed Patitotective, 2021-09-25 14:28
Messages (9)
msg402626 - (view) Author: Cristobal Riaga (Patitotective) * Date: 2021-09-25 14:28
Added `sort_result` parameter (`bool=True`)  on `getmembers` function inside `Lib/inspect.py`, that, as it name says, allows you to `getmembers` result without sorting it.
I'm needed of this and it seems impossible to achieve because of [`367` line](https://github.com/python/cpython/blob/3.9/Lib/inspect.py#L367): 
```py
results.sort(key=lambda pair: pair[0])
```
Any other solution is very welcomed.

(I need it because I'm working on an [API Reference creator](https://github.com/Patitotective/PyAPIReference) and I think it would be better if it the members are ordered in the same order you define them.)
msg402627 - (view) Author: Cristobal Riaga (Patitotective) * Date: 2021-09-25 14:29
Added sort_result parameter (bool=True) on getmembers function inside Lib/inspect.py, that, as it name says, allows you to getmembers result without sorting it.
I'm needed of this and it seems impossible to achieve because of 367 line:

results.sort(key=lambda pair: pair[0])

Any other solution is very welcomed.

(I need it because I'm working on an API Reference creator and I think it would be better if it the members are ordered in the same order you define them.)
msg402637 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2021-09-25 17:31
But you will not get them in creation order, even if omit the final sort, because:

1. dir() returns names sorted alphabetically, not in order of creation.
2. Dynamic class attributes are added at the end, and inherited attributes follow attributes defined in that class.

So adding this parameter will not help you.
msg402675 - (view) Author: Cristobal Riaga (Patitotective) * Date: 2021-09-26 17:15
So there is no way to get the members in order?
msg402676 - (view) Author: Cristobal Riaga (Patitotective) * Date: 2021-09-26 17:16
So there is no way to get members in the order you defined them?
msg402700 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2021-09-27 11:45
It depends on what you want to get. In general, it is difficult if interpret your request literally. For example, if you define class A in module a, class B in module b, and class C which inherits from classes A and B in module c, then what members of C are defined first: inherited from A or inherited from B? Oh, and some members can be defined in a metaclass of A, B or C. And what order of members inherited from the object class or other builtin classes? And what to do with members excluded from __dir__()? And dynamic members provided by __getattr__() or __getattribute__()? You need to specify in more detail what you want to get a meaningful answer. It may be that you actually do not need all these details and can just look in type's dicts in the mro order.
msg402747 - (view) Author: Cristobal Riaga (Patitotective) * Date: 2021-09-27 22:03
I'm don't really need inherit order, neither built-in members.
e.g.:
```py
a = 3 # First

class B: # Second
    pass

def foo(): # Third
    pass

```
Or in a more complex module:
```py
class A: # First
    pass

class B: # Second
    pass

class C(A, B): # Third
    pass
```
The order should be:
- A (class)
- B (class)
- C (class): inherited from A and B.

Here is the link to the script I'm using to inspect a module: https://github.com/Patitotective/PyAPIReference/blob/main/PyAPIReference/inspect_object.py.
msg402748 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2021-09-27 22:06
Ah, if you only need a module, then just use its __dict__ (or vars()).
msg402750 - (view) Author: Cristobal Riaga (Patitotective) * Date: 2021-09-27 22:32
That worked 🙃, thank you for your time and answer.
I will keep working on PyAPIReference.
Also I will close the pull request I created on GitHub.
History
Date User Action Args
2022-04-11 14:59:50adminsetgithub: 89451
2021-10-01 21:59:45terry.reedysetstatus: open -> closed
type: enhancement
stage: resolved
resolution: rejected
versions: + Python 3.11, - Python 3.8
2021-09-27 22:32:05Patitotectivesetmessages: + msg402750
2021-09-27 22:06:50serhiy.storchakasetmessages: + msg402748
2021-09-27 22:03:52Patitotectivesetmessages: + msg402747
2021-09-27 11:45:15serhiy.storchakasetmessages: + msg402700
2021-09-26 17:16:14Patitotectivesetmessages: + msg402676
2021-09-26 17:15:34Patitotectivesetmessages: + msg402675
2021-09-25 17:31:49serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg402637
2021-09-25 14:29:12Patitotectivesetmessages: + msg402627
2021-09-25 14:28:24Patitotectivecreate