Featureintro
The dir() Function
Also called dir, dir(), __dir__, list attributes, module __dir__.
dir() with no argument lists the names in the current local scope. dir(obj) lists the attributes of an object, its class and the base classes, sorted. An object can change the list with __dir__.
- Language
- Python
- Proposals
- pep-562
Synopsis
def f():
x = 1
y = 2
print(dir())
f()
class Point:
dims = 2
def __init__(self, x, y):
self.x = x
self.y = y
def norm(self):
return abs(self.x) + abs(self.y)
p = Point(3, 4)
print([n for n in dir(p) if not n.startswith("_")])
print([n for n in dir(Point) if not n.startswith("_")])
['x', 'y']
['dims', 'norm', 'x', 'y']
['dims', 'norm']
Semantics
r1. "Without arguments, return the list of names in the current local scope. With an argument, attempt to return a list of valid attributes for that object."1 Example: basic.
r2. "If the object has a method named dir(), this method will be called and must return the list of attributes."1 dir() "converts the returned iterable to a list and sorts it."2 Example: custom-dir.
r3. With no __dir__(), dir() uses the __dict__ of the object and its type. For a module, the list has the attributes of the module. For a class, it has the attributes of the class "and recursively of the attributes of its bases." For any other object, it has the attributes of the object, of its class and of the base classes of the class.1
r4. "The resulting list is sorted alphabetically."1
r5. A module can define a function __dir__ with no arguments. "If present, this function overrides the standard dir() search on a module."2 Example: module-dir.
r6. "dir() does not list the names of built-in functions and variables. If you want a list of those, they are defined in the standard module builtins."3 Example: builtins.
r7. dir() is a tool for the interactive prompt. Its "detailed behavior may change across releases."1 Do not use its result as a full list of attributes; use hasattr() or getattr() to test for one attribute.
Examples
class Shape:
def __dir__(self):
return ["perimeter", "area"]
print(dir(Shape()))
['area', 'perimeter']
__dir__ returned the names in any order, and dir() sorted them (rule r2).
import types
m = types.ModuleType("m")
m.a = 1
m.__dir__ = lambda: ["a", "b"]
print(dir(m))
['a', 'b']
A module file does the same thing with def __dir__(): ... at the top level (rule r5).
import builtins
print("len" in dir())
print("len" in dir(builtins))
False
True
Pitfalls
This code runs but gives a wrong answer on purpose.
class Lazy:
def __getattr__(self, name):
if name == "answer":
return 42
raise AttributeError(name)
z = Lazy()
print(z.answer)
print("answer" in dir(z))
42
False
The attribute exists, but dir() cannot see it. "The resulting list is not necessarily complete and may be inaccurate when the object has a custom getattr()."1 Define __dir__ too, or test with hasattr().
This code runs but gives a wrong answer on purpose.
print(hasattr(int, "mro"))
print("mro" in dir(int))
True
False
mro is an attribute of the metaclass type, and "metaclass attributes are not in the result list when the argument is a class."1
Comparison
vars(obj) returns the __dict__ of one object, with no inherited names. dir(obj) merges the names of the object, its class and the base classes into one sorted list (rule r3). See Dynamic attribute access. For a general view of reflection in many languages, see Runtime reflection.
References
- Built-in Functions, Python 3.15 documentation, dir()
- The Python Language Reference, Python 3.15, 3.3.2 Customizing attribute access; 3.3.2.1 Customizing module attribute access
- The Python Tutorial, Python 3.15, 6.3 The dir() Function
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W3 standard |
|
Tag JXYMN7 Edit this page History Report an error Markdown