Quick wins for a faster PC:
Fix the driver behind crashes, sound loss and screen glitchesFind Drivers →Clear out junk files and repair common Windows errorsFree Scan →Scan for outdated or missing drivers - takes under a minuteDriver Scan →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:
The Tool Desk
Outbyte PC Repair FREEClear out junk files and repair common Windows errorsFree Scan →Outbyte Driver Updater FREEScan for outdated or missing drivers - takes under a minuteDriver Scan →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.
#1 Best Overall
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:
Rank #2
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.
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.
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.
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.”
Best Value
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.
Quick Recap
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.




