Creating and Using Custom Objects
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 an object in Python, you often need to know what you can do with it. Can you add items, sort it, or reverse it? 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 an object.
What do you think happens?
What do you think dir() returns when it is used with a list object?
Reveal answer
A list of names
The output of dir() is a list of strings. Each string names an attribute or method available on the object.
Reading the Returned Names
The result of dir() is not a single description of an object. It is a collection of names. Each string in that list represents one capability of the inspected object. A name such as append identifies an available method, while a name such as __len__ identifies a special method associated with a language feature. In this way, dir() acts like an inventory of what the object can expose.
Interpreting a Few Names
Suppose the names append, sort, reverse, and __len__ appear in the result of dir() for a list object. What does each name tell you?
- 1Identify append
The object has an append method, so append is an action that can be called directly on the list.
- 2Identify sort
The object has a sort method, so sorting is one of the operations supported by the list.
- 3Identify reverse
The object has a reverse method, so reversing is another direct operation available on the list.
- 4Identify __len__
The object has the special method used when Python performs the operation associated with
len().
The names are evidence of capabilities on the object. Standard names point to actions you call directly, while the dunder name represents a special method Python uses for a language feature.
Standard and Dunder Names
A dir() listing contains two important categories of names. Standard methods have ordinary names such as append, sort, and reverse. These are user-facing actions that you call directly on an object. Dunder methods have names surrounded by double underscores, such as __add__ and __len__. Python uses these special methods internally to implement core language features.
| Name category | Examples | How it is used |
|---|---|---|
| Standard methods | append, sort, reverse | Called directly as object actions |
| Dunder methods | __add__, __len__ | Usually invoked by Python through operators or built-in functions |
The two categories of names commonly noticed in dir() output
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 what it supports. Inspecting the object gives you a list of names to investigate before you write code that uses it. This can reveal methods and attributes without requiring you to memorize every capability or immediately search external documentation.
The same approach applies to custom objects. Their dir() output can show names supplied by the class or instance, along with capabilities inherited from a parent class. The result reflects the object's state when you inspect it. If an attribute is added dynamically, that name can appear the next time you call dir().
Common Inspection Mistakes
Common mistake Assuming dir() returns a single description
What it looks likeExpecting a sentence that explains the object
Why it failsdir()returns a list of strings, with each string naming a capability.Do this insteadRead the result as an inventory of attribute and method names.
Common mistake Treating every name as a standard method
What it looks likeTreating __len__ in the same way as append
Why it failsDunder methods are special methods that Python uses internally for language features.
Do this insteadSeparate ordinary names from names surrounded by double underscores.
Common mistake Ignoring the connection between a name and an actual capability
What it looks likeSeeing sort in the result but not recognizing that the object supports sorting
Why it failsEach name in the list corresponds to an attribute or method on the object.
Do this insteadUse each returned name as evidence of an operation or property available on that object.
Common mistake Trusting an old inspection after changing the object
What it looks likeContinuing to use a dir() result after adding an attribute dynamically
Why it failsdir()reflects the object's state at the moment it is called.Do this insteadCall dir() again when the object's attributes or structure have changed.
Practice and Summary
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 each name tells you about the object.
Hints
- Look for names surrounded by two underscores.
- Standard names identify actions that can be called directly.
- Dunder names identify special methods associated with Python language features.
- 1
dir()returns a list of strings naming an object's attributes and methods. Standard names such as append, sort, and reverse identify actions that can be called directly. Dunder names such as__add__and__len__identify special methods Python uses internally for operators and built-in functions. Each returned name maps to a capability on the inspected object. Because the result reflects the object's current state,dir()is useful for exploring unfamiliar objects and checking what a custom object can do at a particular moment.
At a glance
- Use
dir()to inspect an object and receive a list of capability names. - Interpret each returned string as the name of an attribute or method on the object.
- Distinguish directly used standard methods from dunder methods used by Python for language features.
- Use
dir()to explore unfamiliar and custom objects during debugging. - Repeat the inspection after changes because
dir()reflects the object's state at inspection time.