Repository navigation
enhanced dir? #91410
Description
Activity
current dir gives output like this,
from collection import OrderedDict od = OrderedDict({'a': 1, 'b': 2, 'c': 3}) print(dir(od))['__class__', '__contains__', '__delattr__', '__delitem__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'items', 'keys', 'move_to_end', 'pop', 'popitem', 'setdefault', 'update', 'values']but wouldn't it be better if the output was like this,
{'OrderedDict': {'__contains__', '__delitem__', '__dir__', '__eq__', '__format__', '__ge__', '__getitem__', '__gt__', '__init__', '__init_subclass__', '__iter__', '__le__', '__lt__', '__ne__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__setitem__', '__sizeof__', '__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'items', 'keys', 'move_to_end', 'pop', 'popitem', 'setdefault', 'update', 'values'}, 'dict': {'__getattribute__', '__len__', '__new__'}, 'object': {'__delattr__', '__setattr__', '__str__'}}???
- added3.11only security fixesonly security fixestype-featureA feature request or enhancementA feature request or enhancement
on Apr 7, 2022 This would be nice, but backward compatibility alone means we can't change dir() to return a dictionary. What you propose would make more sense as a new function, perhaps in a package like pydoc or a third-party tool like IPython.
would adding an argument to dir be a possible solution,
like,l = [1, 2, 3] dir(l, categorize=True)plus I would want it to have some more methods,
l = [1, 2, 3]using enhanced dir should give,
{'list': {'__add__', '__contains__', '__delitem__', '__eq__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__repr__', '__reversed__', '__rmul__', '__setitem__', '__sizeof__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort'}, 'object': {'__delattr__', '__dir__', '__format__', '__reduce__', '__reduce_ex__', '__setattr__', '__str__'}}obtained from, (here only printing, made a dictionary in the implementation in the PR)
for j in dir(l): print(f'l.{j}.__qualname__')this check fails for,
{'list': {'__doc__', '__hash__', '__class__'}}I would also want these,
{'list': {'__instancecheck__','__subclasscheck__', '__subclasses__', 'mro'}, 'type': {'__call__', '__prepare__'}}which were obtained from,
for j in set(dir(type(l))) - set(dir(l)): print(f'l.{j}.__qualname__')and it fails for,
{'list': {'__abstractmethods__', '__base__', '__bases__', '__basicsize__', '__dict__', '__dictoffset__', '__flags__', '__itemsize__', '__module__', '__mro__', '__name__', '__qualname__', '__text_signature__', '__weakrefoffset__'}I would like this enhanced dir to have one more feature, that is the type of these functions, so, our enhanced dir would work something like this,
l = [1, 2, 3] enhanced_dir(l, show_types=True) # one feature is categorize which I showed above, and show_types is the second featurewhich gives,
{ <class 'method-wrapper': {'__add__', '__contains__', '__delattr__', ...} { <class 'builtin_function_or_method': {'__dir__', 'append', 'insert', ...}- added a commit that references this issue
on Apr 11, 2022 i think so one more thing could be added to it, could name the keyword as
checks,
for example,enhanced_dir(None, checks=True)this would run a few checks on our argument, such as, whether it,
- is inheritable
- could be used as a key in a dictionary
- could be passed as an argument to defaultdict
- ...
so, for
None, the output would be something like this,{inheritable: False dict_key: True defaultdict_arg: True, ...}- added a commit that references this issue
on Apr 14, 2022 This is the kind of thing that people implement as a utility and publish on pypl. After a few years, if it's stable and has many users, it becomes possible to discuss adding it to the stdlib (which requires a PEP). You don't start by adding it to the stdlib.
Reacted by Alex Waygood, Jelle Zijlstra and Eric Fahlgrensorry, i am new to this, and dont know much about pypl (or is it pypi?), i created a pull request for this also, and added it to
pydoc.pythat is this,could you inform me where would be the suitable place for the function i created. is this the pypl url?
https://github.com/pypa/warehouse
will i have to create some package and then do something likepip install enhanced_dir, but i have only one function in it.The cpython bug tracker and review system are not the places for learner questions. They are the places where we actually work in cpython.
Try python-list for general python questions and core-mentorship for questions about contributing to python.
Reacted by Jelle Zijlstra and Alex Waygood
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields: