🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEpython

python Documentation

LOADING ENGINE...

dir()

AI & DATA SCIENCE // dir

dir() returns an alphabetical list of the names (attributes and methods) available on an object, or the names in the current scope when called with no arguments.

Syntax

dir()
dir(object)

Deep Dive Course

dir(obj) is primarily an interactive exploration tool: it inspects obj's class and inherited classes and returns a sorted list of every attribute and method name it can find, including dunder methods like __init__ and __repr__. Called with no arguments inside a function or at the module level, it instead lists the names currently defined in that local or global scope.

1Understanding dir()

dir(obj) is primarily an interactive exploration tool: it inspects obj's class and inherited classes and returns a sorted list of every attribute and method name it can find, including dunder methods like __init__ and __repr__. Called with no arguments inside a function or at the module level, it instead lists the names currently defined in that local or global scope.

💡

dir() is best used interactively in the REPL to explore what an unfamiliar object supports — for documentation and precise behavior, help(obj) or the official docs are more reliable than guessing from method names alone.

editor.html
print(dir([]))
localhost:3000

2Practical Example

Here is a real-world application of dir() showing how it is used in production Python code.

editor.html
class Car:
    def drive(self): pass
    def brake(self): pass

public_methods = [name for name in dir(Car) if not name.startswith("__")]
print(public_methods)
localhost:3000

3Best Practices

Follow these guidelines when working with dir():

1. Use dir(obj) at the REPL or in a debugger to discover available methods on unfamiliar objects

2. Filter out dunder methods with a list comprehension when you want to see just the 'public' API

3. Prefer hasattr(obj, 'method_name') over checking membership in dir(obj) when your code needs to make a runtime decision

⚠️

Tip: dir() is best used interactively in the REPL to explore what an unfamiliar object supports — for documentation and precise behavior, help(obj) or the official docs are more reliable than guessing from method names alone.

editor.html
print(dir([]))
localhost:3000

Examples

Example 01Basic Usage
print(dir([]))
Example 02Advanced Example
class Car:
    def drive(self): pass
    def brake(self): pass

public_methods = [name for name in dir(Car) if not name.startswith("__")]
print(public_methods)

Best Practices

  • Use dir(obj) at the REPL or in a debugger to discover available methods on unfamiliar objects
  • Filter out dunder methods with a list comprehension when you want to see just the 'public' API
  • Prefer hasattr(obj, 'method_name') over checking membership in dir(obj) when your code needs to make a runtime decision

Interview Question

What's the difference between using dir(obj) and hasattr(obj, name) to check if an object supports a certain method?

Hint: Think about which one is meant for interactive exploration versus programmatic decisions.

dir(obj) returns a full list of names for humans to browse, and it's not guaranteed to be perfectly complete or fast, since some objects customize it via a __dir__ override. hasattr(obj, name) is meant for programmatic use: it directly attempts to access the attribute, catching any error, and returns a clean True or False, which is the more reliable and idiomatic way to make runtime branching decisions based on what an object supports.

Exercises

MediumPractice using dir() in a real scenario.
View Solution
print(dir([]))

Frequently Asked Questions

What's the difference between using dir(obj) and hasattr(obj, name) to check if an object supports a certain method?

dir(obj) returns a full list of names for humans to browse, and it's not guaranteed to be perfectly complete or fast, since some objects customize it via a __dir__ override. hasattr(obj, name) is meant for programmatic use: it directly attempts to access the attribute, catching any error, and returns a clean True or False, which is the more reliable and idiomatic way to make runtime branching decisions based on what an object supports.

Related Functions

help()hasattr()type()