Python 3 reference · Built-in Functions

Python dir() Function

List the names an object has, such as attributes and methods. With no argument, list the names in the current scope.

Python 3 Since 3.0

Syntax

dir()
dir(object)

Parameters

ParameterDescription
object optional Any object: a module, class, instance, …

Return value

A sorted list of names.

Examples

Explore what a string can doTry it yourself
print([name for name in dir("text") if not name.startswith("_")][:8])

Output

['capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find']
Attributes and methods of your own objectTry it yourself
class Point:
    def __init__(self):
        self.x, self.y = 1, 2
    def norm(self):
        return (self.x ** 2 + self.y ** 2) ** 0.5

print([n for n in dir(Point()) if not n.startswith("__")])

Output

['norm', 'x', 'y']
Search a moduleTry it yourself
import math
print([n for n in dir(math) if n.startswith("is")])

Output

['isclose', 'isfinite', 'isinf', 'isnan', 'isqrt']

Good to know

dir() is meant for exploring in the REPL. In programs, use hasattr(), getattr() or vars() instead.

Version history

VersionChange
Python 3.0Added