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?

@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