Built-in Functions for Inspection
The dir() function reveals all capabilities (methods and attributes) of any Python object by returning a list of strings.
A Capability Inventory
When you work with a Python object, you may need to discover what it can do. Can it add items, sort them, reverse them, or provide other attributes? Instead of memorizing every capability of every object type, you can use the built-in dir() function. It returns a list of strings containing the names of the methods and attributes available on the object.
What do you think happens?
What kind of value does dir() return when you inspect a list object?
Reveal answer
A list of strings
Each string is the name of a capability that the object has. The names include both ordinary methods and double-underscore, or dunder, methods.
A list of strings containing names such as append, sort, reverse, __add__, and __len__. The complete list can contain additional names.Reading the Returned Names
The result of dir(my_list) is not a list of the object's current values. It is a list of names. Each name represents an actual capability available on the object. For example, seeing append tells you that the list has an append method. Seeing sort tells you that the list has a sort method. Seeing an attribute name tells you that the object exposes that attribute. In this way, dir() acts like an inventory of what the object can do or provide at the moment you inspect it.
Interpreting Three Names
Suppose dir(my_list) includes append, sort, and __len__. What does each name tell you?
- 1append
The object has an append method. The method can be called as
my_list.append(5). - 2sort
The object has a sort method. The method can be called as
my_list.sort(). - 3__len__
The object has a dunder method associated with the
len()function. Python uses__len__internally whenlen()is used.
The names returned by dir() are clues to real operations or attributes available on the object.
Standard and Dunder Methods
| Name category | How it appears | How it is used |
|---|---|---|
| Standard method | append, sort, reverse | Called directly as an object action, such as my_list.append(5) |
| Dunder method | __add__, __len__ | Usually used by Python internally for operators or built-in functions |
Two categories of names that can appear in dir() output
Names wrapped in double underscores are called dunder methods, short for double underscore methods. Python uses these special methods to implement core language features. For example, __add__ is what Python calls when you use the plus operator, and __len__ is what Python calls when you use the len() function. You usually use the operator or built-in function rather than calling the dunder method directly.
Exploring an Unfamiliar Object
dir() is especially useful when you receive an object from a library or another part of a program and do not yet know which operations it supports. Inspect the object first, read the returned names, and then investigate a capability that matches your goal. For example, if the returned names include append, sort, or reverse, those names identify actions available on the object.
The inspection call does not perform the listed actions. It reveals their names. You still choose whether to call a standard method such as append, sort, or reverse, depending on what you want the object to do.
Inspecting the Current State
dir() reflects the actual state of an object at the moment you call it. If an attribute is added to an object dynamically, that name appears the next time you inspect the object. This makes dir() useful during debugging: it can show which capabilities are available now and help you notice when an expected capability is missing.
Using an Inspection Result as a Snapshot
You inspect an object and do not see a capability you expected. What should you consider?
- 1Check the object
Confirm that you are inspecting the object you intended to use.
- 2Consider its type
The object might be a custom object with dynamically added attributes, or it might be a subclass that inherits capabilities from a parent class.
- 3Inspect again
Call
dir()again if the object's state may have changed. The result represents the capabilities available at that moment.
A dir() result is a current snapshot of available names, not a permanent description of every object you might later work with.
Mistakes with dir()
Common mistake Treating dir() as if it returns the object's data values
What it looks likeExpecting dir(my_list) to return 3, 1, and 2Why it failsdir()returns a list of strings containing capability names, not the current values stored in the object.Do this insteadRead each returned string as the name of an attribute or method available on the object.
Common mistake Assuming every returned name is a standard method to call directly
What it looks likeCalling __len__ by name simply because it appears in the result
Why it failsDunder methods are used by Python internally to implement features such as operators and built-in functions.
Do this insteadRecognize names such as __len__ as special methods and understand that len() uses __len__ behind the scenes.
Common mistake Assuming the output contains only familiar actions
What it looks likeIgnoring names such as __add__ and __len__ because they do not look like append or sort
Why it failsdir()includes both standard methods and dunder methods.Do this insteadSeparate ordinary names from double-underscore names when reading the result.
Common mistake Treating one inspection result as permanent
What it looks likeAssuming the list of names cannot change after an attribute is added dynamically
Why it failsdir()reflects the object's actual state when it is called.Do this insteadInspect the object again after its available attributes or capabilities may have changed.
Practice and Takeaways
Imagine that dir() returns the names append, reverse, __add__, and __len__ for an object. Classify each name as a standard method or a dunder method, then explain what the presence of append and __len__ tells you about the object.
Hints
- Look for names wrapped in double underscores.
- A standard method is an ordinary object action that can be called directly.
- The source explains that
__len__is associated with thelen()function.
- 1
dir()returns a list of strings naming the capabilities available on a Python object. The result includes both standard methods, such as append, sort, and reverse, and dunder methods, such as__add__and__len__. Each returned name corresponds to an actual attribute or method available through the object. Usedir()to explore unfamiliar objects, interpret unexpected capabilities, and inspect the object's current state while debugging.
At a glance
dir()returns a list of strings naming an object's available methods and attributes.- Ordinary names such as append, sort, and reverse identify standard methods that can be called directly.
- Names surrounded by double underscores identify dunder methods that Python uses internally for features such as operators and built-in functions.
- Each name in the result maps to an actual capability on the inspected object.
dir()is useful for exploring unfamiliar objects and checking their current capabilities during debugging.