Python Reference Library →

Python dir() Function – List Attributes of Objects

Let’s look at dir() function – list attributes of objects

  1. Notice

    dir() lists names associated with an object. Its result is useful for exploration, not a guarantee about every possible dynamic attribute.

  2. Try

    Follow the example, change its sample, and compare what happens.

  3. Check

    Explain the result and test your understanding.

Watch how it works

Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.

Explore the idea

Follow the code and watch its state change. Guided simulation.

Code
Python
items = [3]
names = dir(items)
print("append" in names)
print("items" in names)
Live state

Top-level scope

items→Object 1
names→Object 2
Object 1 · list[3]Object 2 · list['__add__', '__class__', '__class_getitem__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', …]
This step

dir() lists names associated with an object. Its result is useful for exploration, not a guarantee about every possible dynamic attribute.

Picture dir() function – list attributes of objects

dir() lists names associated with an object. Its result is useful for exploration, not a guarantee about every possible dynamic attribute.

Image brief

Alt text
A diagram for Python dir() Function – List Attributes of Objects. Instructions: items = [3] names = dir(items) print("append" in names) print("items" in names). Displayed result: True False. The final example bindings are items --> object 1 (list); names --> object 2 (list). object 1: [3]; object 2: ['__add__', '__class__', '__class_getitem__', '__contains__', ...
Image generation prompt
Create a clean educational illustration for a Python programming lesson titled "Python dir() Function – List Attributes of Objects". Teaching focus: dir() lists names associated with an object. Its result is useful for exploration, not a guarantee about every possible dynamic attribute. Depict exactly the relationships and labels in this reference diagram: EXAMPLE INSTRUCTIONS items = [3] names = dir(items) print("append" in names) print("items" in names) | v DISPLAYED RESULT True False FINAL BINDINGS items --> object 1 (list) names --> object 2 (list) object 1: [3] object 2: ['__add__', '__class__', '__class_getitem__', '__contains__', ... Use a flat vector style, generous spacing, clear arrows and legible labels, with a restrained blue/teal palette on a plain background. The diagram must explain the programming idea accurately. Do not depict variables as boxes containing copied objects when they share a reference. Avoid mascots, decorative screenshots, logos, gradients and extra text. Landscape composition, approximately 3:2, suitable for a course card. Accessible description: A diagram for Python dir() Function – List Attributes of Objects. Instructions: items = [3] names = dir(items) print("append" in names) print("items" in names). Displayed result: True False. The final example bindings are items --> object 1 (list); names --> object 2 (list). object 1: [3]; object 2: ['__add__', '__class__', '__class_getitem__', '__contains__', ...

Follow the execution

Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.

Execution workspace

Choose a sample and follow its execution. Guided simulation.

Code
Python
items = [3]
names = dir(items)
print("append" in names)
print("items" in names)
Live state

Top-level scope

items→Object 1
names→Object 2
Object 1 · list[3]Object 2 · list['__add__', '__class__', '__class_getitem__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', …]
This step

The example has finished. Compare the final output with the values and references above.

Terminal
Output
True
False

What do you expect this program to print?

What does this example display?

items = [3]
names = dir(items)
print("append" in names)
print("items" in names)
What does this example display?
Choose one answer. You can try again.

Answer and explanation

True False

Follow the printed values in order. The output is: True False

More examples and reference

Python dir() Function

The dir() function is a built-in Python tool that returns a list of valid attributes (including methods) for any object. It's often used for exploring objects during development or debugging.

Syntax

dir([object])

Parameters:

  • object (optional) – the object whose attributes you want to list

Returns:

  • A list of strings – each representing a valid attribute name of the object

Example 1: Using dir() without Arguments

When used without arguments, dir() returns the names in the current local scope.

x = 10
y = "hello"
print(dir())
['__annotations__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', 'x', 'y']

Example 2: Using dir() with a Built-in Object

print(dir("hello"))

Output (partial):

['__add__', '__class__', '__contains__', 'capitalize', 'casefold', 'center', 'count', ...]

This shows all the string methods and internal attributes.

Example 3: Using dir() with a Module

import math
print(dir(math))

Output (partial):

['__doc__', '__loader__', 'acos', 'asin', 'atan', 'ceil', 'cos', 'pi', 'sqrt', ...]

Use Cases of dir()

  • Exploring unknown objects or modules during development
  • Quick introspection without reading documentation
  • Understanding what operations can be done on an object

Common Mistakes

  • Not all returned items are methods – some are special/internal attributes
  • dir() doesn't always list everything – some attributes added via __getattr__ may not appear

Interview Tip

Use dir() along with type() and help() for debugging and exploring unfamiliar objects during coding interviews or while using third-party libraries.

Summary

  • dir() helps inspect available attributes and methods of objects
  • With no arguments, it shows names in the current local scope
  • It's useful for exploring built-in types, custom classes, and modules

Practice Task

Try running dir() on a few different objects like [], {}, set(), and len. Observe how their available methods and properties differ.

print("List Methods:", dir([]))
print("Dict Methods:", dir({}))
print("Set Methods:", dir(set()))
print("Function Methods:", dir(len))

What to remember

Connect what you noticed in the example with the idea below.

Remember

dir() lists names associated with an object. Its result is useful for exploration, not a guarantee about every possible dynamic attribute.

Watch out for this idea

dir() changes the object’s attributes.

This is the misconception to avoid.

Reference checks are optional and do not count toward course progress.

    Reference library →