Concepts / Understanding Objects and Types

Understanding Objects and Types

The dir() function reveals all capabilities (methods and attributes) of any Python object by returning a list of strings.

  • Programming

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?

  • A single string
  • A number
  • A list of strings
  • Only the list's current values
Reveal answer

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__.

inspectreturnslist objectdir(list object)inspectionlist of stringscapability names
How does information move from an object into the list returned by dir()?

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.

dir() listsnamesdir() listsnamesmy_listlist objectappendname in dir()my_list.appendmethodsortname in dir()my_list.sortmethod
How does each name returned by dir() connect to an actual attribute or method that can be accessed on the object?
python

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 categoryExamplesHow Python uses it
Standard methodsappend, sort, reverseThese 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.
includesincludesincludesincludesStandard methodsdirect actionsappend__add__+ operatorsortDunder methodsinternal language behavior__len__len() function
What is the difference between ordinary method names and double-underscore names 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

  1. Start with the object you have received or are currently working with.
  2. Call dir() on that object to obtain a list of capability names.
  3. Look through the names for standard methods or attributes related to the operation you need.
  4. Use a matching name on the object, such as my_list.append(), when the name represents a method you want to call.
  5. Use 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.

Inspect: Call dir() on the list object. The result is a list of strings containing the object's capability names.

Find names: Look for append, sort, and reverse in the returned names. These are standard methods shown in the source example.

Use 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

  • Assuming dir() returns the object's values.

    dir() returns a list of strings containing names of capabilities, methods, and attributes.

    Fix: Read each returned item as a name that identifies something available on the object.

  • Treating a returned name as though it were already a method call.

    The returned string identifies the capability; it is not itself the object method being called.

    Fix: Use the name with the object when you want to access the corresponding method.

  • Assuming every name should be called directly.

    Dunder methods are used internally by Python for features such as len() and the + operator.

    Fix: Distinguish dunder names from standard methods before deciding how to use a capability.

  • Treating one dir() result as permanent.

    dir() reflects the object's state when it is called, and dynamically added attributes can appear in a later result.

    Fix: Call dir() again when the object changes or when the current capability list seems unexpected.

Practice with dir()

EASY

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.

append: This is a standard method name. It indicates that my_list has an append method that can be accessed as my_list.append().

reverse: This is another standard method name. It indicates that my_list has a reverse method that can be accessed as my_list.reverse().

__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.
  2. Standard names such as append, sort, and reverse represent direct actions available on an object.
  3. Dunder names such as __add__ and __len__ are special methods Python uses internally for operators and built-in functions.
  4. Each returned name corresponds to an actual capability that can be accessed on the object.
  5. Use dir() to explore unfamiliar objects, investigate unexpected capabilities, and inspect the object's current state.

Key Takeaways

  • 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.