October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk3 min

Python `dir()` Function: A Practical Guide With Examples

Python’s dir() is an alphabetized discovery aid for local names and object attributes, not a guaranteed complete inventory. See examples and its limitations.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Python’s dir() helps you discover names while exploring code. With no argument, it lists names in the current local scope; with an object, it attempts to list that object’s attribute names. The result is alphabetically sorted, but it is a convenience for exploration—not a guaranteed inventory of everything you can access.

What does Python dir() do?

dir() has two common forms:

Form What it lists
dir() Names in the current local scope.
dir(object) An attempted list of valid attribute names for the object.

For either form, the returned names are sorted alphabetically. Python’s documentation describes dir() as a prompt-oriented discovery aid: it favors an interesting, useful set of names over a rigorously complete and consistent inventory. Python built-in functions documentation

As an Amazon Associate I earn from qualifying purchases.

How do I use dir()?

Inspect names in the current local scope

Call it without an argument to see names available in the current local scope:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
dir()

The exact names depend on the scope and execution context. Python 3.14.7’s built-in functions documentation notes a change in Python 3.13 for calling dir() without an argument and points to the updated behavior described for locals(). The note does not, by itself, specify further details, so scope-sensitive results should be interpreted in the context of the Python version being used.

Inspect a module

Pass a module to explore its reported attribute names. The official documentation demonstrates this with struct:

import struct

dir(struct)

Inspect a custom object

For an object, dir(object) attempts to show names associated with it. For example, this class customizes the list:

class Shape:
    def __dir__(self):
        return ["area", "perimeter", "location"]

shape = Shape()
dir(shape)
# ['area', 'location', 'perimeter']

The custom method supplies the names, and dir() returns them in alphabetical order.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

What names appear for different object types?

  • Modules: reported module attribute names.
  • Class objects: class and base-class attributes.
  • Other objects: instance names together with class and base-class names.

These are broad patterns, not a promise that every reported name can always be retrieved or that every accessible name will be reported.

What is __dir__() in Python?

__dir__() is an object’s hook for customizing the names shown by dir(object). If the object defines it, dir() calls it. That means the displayed list can reflect the object’s chosen presentation rather than a complete account of its attributes.

If no __dir__() is defined, Python tries to collect names from the object’s __dict__, if it has one, and from its type. The resulting list can be incomplete or inaccurate, particularly when custom __getattr__() behavior provides attributes dynamically.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Why does dir() not show every attribute?

Its output depends on the object’s type and any customization, and Python explicitly treats the detailed behavior as a convenience rather than a strict inventory. For example, when the argument is a class, attributes on its metaclass do not appear in the result. A name missing from dir() therefore does not prove that the name cannot be accessed; likewise, a listed name is not an unconditional guarantee of successful retrieval.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Python’s documentation summarizes the design this way: “Because dir() is supplied primarily as a convenience for use at an interactive prompt, it tries to supply an interesting set of names more than it tries to supply a rigorously or consistently defined set of names, and its detailed behavior may change across releases.”

When should you use dir()?

Use it when exploring an unfamiliar module or object interactively, or when you want a quick view of names exposed by a scope. Treat its output as a starting point for inspection, not as a specification of the object’s full interface. For code that depends on a particular attribute, check that attribute directly and handle the possibility that it is absent or behaves dynamically.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.