Understanding Objects and Types
Think of dir() as an inventory of an object's capabilities. It does not require you to memorize every capability of every object type before you begin exploring.
A Capability Inventory
When you work with an object, you may need to know what operations it supports. For example, you might want to add items, sort values, or reverse a collection. Python's built-in dir() function helps you discover these capabilities by returning a list of strings containing names of the object's methods and attributes.
Think of dir() as an inventory of an object's capabilities. It does not require you to memorize every capability of every object type before you begin exploring.
Following dir() Output
What do you think happens?
What do you think dir() returns when it is called on 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, such as append and sort, and dunder methods, such as __add__ and __len__.
The flow is simple: provide an object to dir(), and Python returns a list of strings. The list contains names rather than a description of how each capability works. You read those names as an inventory of what the object can do or what attributes it possesses.
Reading the Returned Names
Each string in the result corresponds to an actual capability on the inspected object. If append appears in the list for a list object, that object has an append method. If sort appears, the object has a sort method. The name is the part you use to identify the capability; calling the method requires accessing that name on the object.
These calls illustrate the practical meaning of names found in dir() output. Seeing append tells you that my_list.append() is available; seeing sort tells you that my_list.sort() is available; and seeing reverse tells you that my_list.reverse() is available.
Standard and Dunder Names
| Name category | Examples | How Python uses it |
|---|---|---|
| Standard methods | append, sort, reverse | These are user-friendly actions you call directly on the object. |
| Dunder methods | __add__, __len__ | Python uses these special methods internally for features such as the + operator and the len() function. |
Two categories of names that can appear in dir() output
Dunder is short for double underscore. Dunder names are recognizable because they begin and end with two underscores, as in __add__ and __len__. Python uses these special methods internally to implement core language features. For example, __add__ is used when the + operator is applied, and __len__ is used when the len() function is used. Standard methods such as append, sort, and reverse are the names you normally call directly.
Exploring an Unfamiliar Object
- 1Start with the object you have received or are currently working with.
- 2Call
dir()on that object to obtain a list of capability names. - 3Look through the names for standard methods or attributes related to the operation you need.
- 4Use a matching name on the object, such as
my_list.append(), when the name represents a method you want to call. - 5Use the result as a current snapshot of the object's capabilities and inspect it again if the object changes.
Finding a List Operation
You are working with a list and want to discover whether it supports operations for adding, sorting, or reversing items.
- 1Inspect
Call
dir()on the list object. The result is a list of strings containing the object's capability names. - 2Find names
Look for append, sort, and reverse in the returned names. These are standard methods shown in the source example.
- 3Use a capability
A name such as append corresponds to the actual method
my_list.append(), so it can be called with an item such as 5.
dir() lets you discover the relevant list methods without first memorizing every capability of the list object.
This makes dir() useful when an object comes from a library or another part of a program and you are not yet familiar with it. Instead of beginning by searching external documentation, you can inspect the object immediately and use the returned names to guide your next step.
Changing Object Capabilities
dir() reflects the actual state of an object at the moment you call it. If an attribute is added to an object dynamically, that attribute appears the next time you call dir(). This means the result should be understood as a current snapshot, not as a permanently fixed description.
Common Inspection Mistakes
Common mistake Assuming dir() returns the object's values.
What it looks likeExpecting dir(my_list) to return the items stored in my_list.
Why it failsdir()returns a list of strings containing names of capabilities, methods, and attributes.Do this insteadRead each returned item as a name that identifies something available on the object.
Common mistake Treating a returned name as though it were already a method call.
What it looks likeSeeing append in the result and forgetting that the object method is accessed as my_list.append().
Why it failsThe returned string identifies the capability; it is not itself the object method being called.
Do this insteadUse the name with the object when you want to access the corresponding method.
Common mistake Assuming every name should be called directly.
What it looks likeTreating __len__ in the same way as the standard method append.
Why it failsDunder methods are used internally by Python for features such as
len()and the + operator.Do this insteadDistinguish dunder names from standard methods before deciding how to use a capability.
Common mistake Treating one dir() result as permanent.
What it looks likeIgnoring a newly added attribute because it was not present in an earlier inspection.
Why it failsdir()reflects the object's state when it is called, and dynamically added attributes can appear in a later result.Do this insteadCall dir() again when the object changes or when the current capability list seems unexpected.
Practice with dir()
Suppose dir(my_list) includes append, reverse, and __len__. Explain what each name tells you about my_list, and identify which names are standard methods and which name is a dunder method.
Hints
- Remember that
dir()returns names as strings. - A standard method is a direct action such as append or reverse.
- A dunder method has double underscores and is used internally for a core language feature.
Practice Answer
Interpret append, reverse, and __len__ in a dir() result for my_list.
- 1append
This is a standard method name. It indicates that
my_listhas an append method that can be accessed asmy_list.append(). - 2reverse
This is another standard method name. It indicates that
my_listhas a reverse method that can be accessed asmy_list.reverse(). - 3__len__
This is a dunder method name. Python uses it internally when the
len()function is used.
The names identify capabilities on my_list, while their naming patterns help distinguish direct standard methods from internally used dunder methods.
Key Takeaways
- 1
dir()returns a list of strings that names an object's methods and attributes. - 2Standard names such as append, sort, and reverse represent direct actions available on an object.
- 3Dunder names such as
__add__and__len__are special methods Python uses internally for operators and built-in functions. - 4Each returned name corresponds to an actual capability that can be accessed on the object.
- 5Use
dir()to explore unfamiliar objects, investigate unexpected capabilities, and inspect the object's current state.
At a glance
dir()provides a list of strings describing an object's capabilities.- Names such as append and sort are standard methods that you call directly.
- Names surrounded by double underscores are dunder methods that Python uses internally.
- A name in
dir()maps to a corresponding attribute or method on the inspected object. dir()is a practical discovery and debugging tool, especially for unfamiliar or changing objects.