Methods and Attributes: Accessing Object Capabilities
The dir() function reveals all capabilities (methods and attributes) of any Python object by returning a list of strings.
The Inspection Question
When you work with an object, you often need to know what you can do with it. Can you add items, sort it, or reverse it? Python provides the built-in dir() function for this kind of inspection. It reveals an object's capabilities by returning a list of strings containing names of its methods and attributes.
What do you think happens?
What do you think dir() returns when it is applied to a list object?
Reveal answer
A list of capability names
The output of dir() is a list of strings. Each string names one capability that the inspected object has.
From Object to Capability List
Calling dir() on an object produces an inventory of names. The object is the thing being inspected, and the returned list is the inspection result. Each string in that list represents one capability associated with the object at the moment dir() is called.
Reading a List Inventory
Suppose the result of inspecting a list includes append, sort, reverse, and __len__. What does each name tell you?
- 1Read append
The list has an append method. This is a direct action that can be called on the list.
- 2Read sort
The list has a sort method, so sorting is one of its available capabilities.
- 3Read reverse
The list has a reverse method, so reversing is one of its available capabilities.
- 4Read __len__
The list has the special method associated with Python's
len()function.
The list of strings acts as an inventory: each name points to one capability of the inspected list.
Ordinary and Dunder Names
The names returned by dir() include two important categories. Standard method names, such as append, sort, and reverse, describe user-facing actions that you call directly on an object. Dunder names, such as __add__ and __len__, begin and end with double underscores. Python uses these special methods internally to implement core language features.
| Name in dir() output | Category | Role |
|---|---|---|
| append | Standard method | A direct action called on a list |
| sort | Standard method | A direct action called on a list |
| reverse | Standard method | A direct action called on a list |
| __add__ | Dunder method | A special method Python uses for the plus operator |
| __len__ | Dunder method | A special method Python uses for the len() function |
Common categories visible when inspecting a list
Exploring an Unfamiliar Object
dir() is useful when you receive an object from a library or another part of a program and do not yet know what it supports. Inspecting the object gives you a list of names to investigate. You can look for familiar operations such as append, sort, or reverse, and you can recognize special names such as __len__ as evidence that the object supports a related Python feature.
An object has its current capabilities.
The object's available methods and attributes have not yet been inspected.
dir() is called, producing a list of capability names.
The current capabilities are represented as strings that can be examined.
The result of dir() reflects the object at the moment you inspect it. If an attribute is added dynamically to an object, that attribute appears in the result the next time dir() is called. Unexpected names can also occur because a custom object may have dynamically added attributes or because a subclass inherits capabilities from a parent class.
Mistakes in Reading dir()
Common mistake Expecting dir() to return one descriptive string
What it looks likeTreating the inspection result as a single label for the object
Why it failsdir()returns a list of strings, with each string naming one capability.Do this insteadRead the result as an inventory of separate names.
Common mistake Treating every returned name as a standard method
What it looks likeReading __len__ in exactly the same way as append
Why it failsDunder methods are special methods Python uses internally, while append is a standard method called directly.
Do this insteadCheck whether the name begins and ends with double underscores, then identify whether it is a standard or special method.
Common mistake Assuming the result contains only familiar operations
What it looks likeBeing surprised by inherited names or dynamically added attributes
Why it failsdir()reflects the capabilities associated with the object being inspected, including capabilities introduced through custom objects, inheritance, or later changes.Do this insteadTreat unexpected names as clues about the object's structure and current state.
Suppose an inspection result contains append, reverse, __add__, and __len__. Classify each name as a standard method or a dunder method. Then explain what capability each name indicates.
Hints
- Look for double underscores at the beginning and end of a name.
- append and reverse are direct list actions.
__add__is associated with the plus operator, and__len__is associated withlen().
Key Takeaways
- 1
dir()returns a list of strings naming an object's capabilities. Standard names such as append, sort, and reverse represent direct actions that you can call on an object. Dunder names such as__add__and__len__are special methods Python uses internally for operators and built-in functions. Each name in the result corresponds to a capability associated with the object, and the result reflects the object's state whendir()is called. Usedir()to explore unfamiliar objects and investigate unexpected behavior.
At a glance
dir()returns a list of strings representing an object's methods and attributes.- Standard methods such as append, sort, and reverse are direct object actions.
- Dunder methods such as
__add__and__len__support Python operators and built-in functions. - Each returned name maps to a capability associated with the inspected object.
dir()is a practical discovery and debugging tool for unfamiliar or changing objects.