When introspecting a #Python object, I sometimes use a comprehension to remove dunder method noise from dir(...) output:

>>> numbers = [2, 1, 3]
>>> public = [name for name in dir(numbers) if not name.startswith("_")]
>>> public
['append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']

This removes MANY underscore-prefixed methods:

>>> len(dir(numbers)) - len(public)
37

Anyone know if something like dir(..., public=True) has ever been proposed?

Found this previous discussion that didn't go anywhere:
https://discuss.python.org/t/add-a-flag-hide-magic-names-in-dir/7276

Maybe worth revisiting?

Some counter-arguments I imagine would crop up:
1. what comes back from __dir__ should be up to the object and Python shouldn't customize it at all
2. assuming that an underscore prefix has special meaning may be inaccurate. As an example, there's a "public" _astuple() method on namedtuples.
3. It's ambiguous. Why check for one underscore instead of 2? And are unofficial dunders removed?

Add a flag "hide magic names" in dir

Hi. While I understand the current behavior of dir(), most of the time I use it is to quickly have a glance of the β€œnormal” exposed names. I would propose to add a keyword-only, default-is-current behavior, flag to allow the caller to show only the β€œnormal” names (i.e. - no dunder stuff). For the name of the flag, I think this is the most complex thing πŸ™‚ and I propose to change the signature to dir([object], *, show_dunder=True) Additionally (but I’m not sure about the back-com...

Discussions on Python.org
@treyhunner I'd go a stage further ...dir(..., public=True, pretty=True) ... where making the pretty arg True was the equivalent of pprint.pprint(dir(...)) ... I find scanning across lines harder than scanning down.

@shearichard dir() returns a list, so I don't think that would work unfortunatley.

But you might find the recently proposed PEP 813 interesting: https://discuss.python.org/t/pep-813-the-pretty-print-protocol/106242

PEP 813 - The Pretty Print Protocol

A couple of the elder statesmen are happy to announce PEP 813, a proposal to build-in optional pretty printing for print(), str.format(), and f-strings, and to define a protocol classes can implement to participate in and customize how their instances are pretty printed. Enjoy, and let the games begin!

Discussions on Python.org
@treyhunner Perhaps this is a naive question, but why remove the "noise" from certain objects? To reduce their size? To eliminate edge effects? For shadowing?

@tech_guillaume This doesn't change an object's size or affect the object at all. The new dir() function would work something like this: https://pym.dev/p/26naj/

It's just saying "don't show me those underscore-prefixed attributes".

That [... for ... in ...] thing is a comprehension: https://pym.dev/what-are-list-comprehensions/

What if the dir function could reduce the noise from underscore-prefixed attributes? - Python Pastebin - Python Morsels

What if the dir function could reduce the noise from underscore-prefixed attributes?

@treyhunner ahh thank you for the precisions, i didn't understand the first assignment