Concepts / Methods and Attributes: Accessing Object Capabilities

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.

  • Programming

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?

  • One descriptive string
  • A number
  • A list of capability names
Reveal answer

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.

dir() namesdir() namesdir() namesdir() nameslist objectinspected objectappendmethodsortmethodreversemethod__len__special method
How does each string returned by dir() correspond to something available on the inspected object?

Reading a List Inventory

Suppose the result of inspecting a list includes append, sort, reverse, and __len__. What does each name tell you?

Read append: The list has an append method. This is a direct action that can be called on the list.

Read sort: The list has a sort method, so sorting is one of its available capabilities.

Read reverse: The list has a reverse method, so reversing is one of its available capabilities.

Read __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() outputCategoryRole
appendStandard methodA direct action called on a list
sortStandard methodA direct action called on a list
reverseStandard methodA direct action called on a list
__add__Dunder methodA special method Python uses for the plus operator
__len__Dunder methodA special method Python uses for the len() function
includesincludesincludesincludesStandard methodsdirect object actionsappendcalled directly__add__plus operatorsortcalled directlyDunder methodsused internally by Python__len__len() function
What is the difference between ordinary names such as append and special names such as __len__?

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.

applyreturnsguidesunfamiliar objectobject to inspectdir()inspection steplist of stringscapability namesnext capabilityoperation to explore
What happens when dir() is applied to an unfamiliar object, and how does its result guide exploration?

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()

  • Expecting dir() to return one descriptive string

    dir() returns a list of strings, with each string naming one capability.

    Fix: Read the result as an inventory of separate names.

  • Treating every returned name as a standard method

    Dunder methods are special methods Python uses internally, while append is a standard method called directly.

    Fix: Check whether the name begins and ends with double underscores, then identify whether it is a standard or special method.

  • Assuming the result contains only familiar operations

    dir() reflects the capabilities associated with the object being inspected, including capabilities introduced through custom objects, inheritance, or later changes.

    Fix: Treat unexpected names as clues about the object's structure and current state.

EASY

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 with len().

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 when dir() is called. Use dir() to explore unfamiliar objects and investigate unexpected behavior.

Key Takeaways

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