Concepts / Creating and Using Custom Objects

Creating and Using Custom Objects

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 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?

  • A single string

  • A number

  • A list of names

  • A description written in sentences

Reveal answer
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.

list objectdir() resultlist of stringsappendmethodsortmethod__len__special methodinspectnamesnamesnames
How do names returned by dir() connect to attributes and methods that can be accessed on the object?
Worked example

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?

  1. 1
    Identify append

    The object has an append method, so append is an action that can be called directly on the list.

  2. 2
    Identify sort

    The object has a sort method, so sorting is one of the operations supported by the list.

  3. 3
    Identify reverse

    The object has a reverse method, so reversing is another direct operation available on the list.

  4. 4
    Identify __len__

    The object has the special method used when Python performs the operation associated with len().

Result

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 categoryExamplesHow it is used
Standard methodsappend, sort, reverseCalled 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

Standard methodsappenddirect action__len__language featuredescribedirect actionDunder methods__str__language featurecontainscontainscontainscontains
What is the difference between ordinary method names and dunder method names in the same dir() listing?

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.

Unfamiliar objectCall dir()inspect namesList of namesattributes and methodsChoose a capabilityUse the capabilityexplorereturnsreviewselect
How does inspecting dir() help identify what an unfamiliar object can do before you use it?

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 like

    Expecting a sentence that explains the object

    Why it fails

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

    Do this instead

    Read the result as an inventory of attribute and method names.

  • Common mistake

    Treating every name as a standard method

    What it looks like

    Treating __len__ in the same way as append

    Why it fails

    Dunder methods are special methods that Python uses internally for language features.

    Do this instead

    Separate ordinary names from names surrounded by double underscores.

  • Common mistake

    Ignoring the connection between a name and an actual capability

    What it looks like

    Seeing sort in the result but not recognizing that the object supports sorting

    Why it fails

    Each name in the list corresponds to an attribute or method on the object.

    Do this instead

    Use 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 like

    Continuing to use a dir() result after adding an attribute dynamically

    Why it fails

    dir() reflects the object's state at the moment it is called.

    Do this instead

    Call dir() again when the object's attributes or structure have changed.

Practice and Summary

EASY

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

Related concepts

More concepts starting with C