The dir() function is a built-in Python function that returns a sorted list of names. Called with an object, it lists the attributes and methods that object exposes. Called with no argument, it lists the names defined in the current scope. It is mainly an interactive tool for exploring unfamiliar objects, modules and classes.
dir() is meant for inspection and debugging, not for exact reflection. Its result is a best-effort list, and a class can customise it. When you need precise data, use vars(), inspect, or getattr() instead.
The signature is dir([object]). The argument is optional and can be any object: a module, class, instance, function or built-in value. The return value is always a list of strings sorted alphabetically.
When run as a script, the exact set of dunder names varies slightly between runs (a script has __file__, an interactive session does not). The names x and name are the ones you defined.
The behaviour depends on the kind of argument, as described in the language reference:
| Argument | What dir() returns |
|---|---|
| None | Names in the current local scope |
| Module | The names in the module namespace |
| Class | Attributes of the class and, recursively, of its base classes |
| Instance | Instance attributes, plus class attributes and those of its base classes |
Object with __dir__() | Whatever __dir__() returns, converted to a sorted list |
Python calls the object's __dir__() method if one exists and then sorts the result. Without a custom __dir__(), it uses the default implementation built from __dict__ and the class hierarchy. Because of this, the result is not guaranteed to be complete or exact, particularly for classes with a custom __getattr__() or a metaclass.
Passing a string shows its methods. To keep the output readable, filter out the dunder names.
The same approach works for modules. This lists the public names in math:
The list grows with each Python release. For example, math.cbrt() appeared in Python 3.11, so the exact output depends on your interpreter version.
For a custom class, dir() includes your attributes along with everything inherited from object.
The instance attribute name, the method speak and the class attribute species all appear because dir() merges the instance and its class.
__dir__()A class can define __dir__() to control what dir() reports. This is useful for objects that create attributes dynamically through __getattr__().
Without the custom __dir__(), host and port would be missing, and tab completion in a REPL or notebook would not suggest them.
These tools overlap but answer different questions.
| Tool | Returns | Includes inherited names | Best for |
|---|---|---|---|
dir(obj) | Sorted list of names | Yes | Quick exploration |
vars(obj) | The __dict__ mapping (names and values) | No | Reading an instance's own attributes |
inspect.getmembers(obj) | List of name and value pairs | Yes | Programmatic inspection |
hasattr(obj, name) | Boolean | Yes | Checking a single attribute |
vars() shows only what is stored in the instance dictionary, with values. It raises TypeError for objects without a __dict__, such as an int or an instance of a class that uses __slots__.
Typical uses are:
getattr() to call or read attributes by name.Do not use dir() to decide program behaviour in production code. Prefer explicit checks such as hasattr(), isinstance(), protocols or typing.Protocol, because the output of dir() can differ between versions and objects. For documentation of an object, pair it with help(obj), which prints the docstrings of those names.
It returns a sorted list of the names in the current local scope, such as variables, functions and imported modules. Inside a function, that means the function's local names.
It shows names that start with an underscore, including dunder methods, because Python has no true private attributes. Filter them with not name.startswith("_") if you only want the public API.
dir() returns a sorted list of names including inherited ones, while vars() returns the object's __dict__ with values and no inherited names. Use vars() for an instance's own data.
Yes. The documentation states that its output is a best-effort list and that a custom __dir__(), __getattr__() or a metaclass can change it. Treat it as a debugging aid.
dir() returns a sorted list of names for the current scope or for any object you pass in. It combines instance, class and base class attributes, and you can customise it with __dir__(). Use it to explore objects interactively, filter dunder names for readability, and reach for vars(), inspect.getmembers() or hasattr() when you need exact, programmatic answers.