Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (Cyborg)
  • No Skin
Collapse
Brand Logo

CIRCLE WITH A DOT

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

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

Scheduled Pinned Locked Moved Uncategorized
python
2 Posts 1 Posters 0 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • treyhunner@mastodon.socialT This user is from outside of this forum
    treyhunner@mastodon.socialT This user is from outside of this forum
    treyhunner@mastodon.social
    wrote last edited by
    #1

    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 beet proposed?

    treyhunner@mastodon.socialT 1 Reply Last reply
    1
    0
    • treyhunner@mastodon.socialT treyhunner@mastodon.social

      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 beet proposed?

      treyhunner@mastodon.socialT This user is from outside of this forum
      treyhunner@mastodon.socialT This user is from outside of this forum
      treyhunner@mastodon.social
      wrote last edited by
      #2

      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?

      1 Reply Last reply
      0
      • R relay@relay.publicsquare.global shared this topic
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups