dir
List the names of the attributes and methods of an object.
Python 3.13
dir([object])Parameters
- objectAnyoptional
- An object, module or class; without it, the names in the current scope.
Return value
An alphabetically sorted list of name strings.
Examples
Attributes and methods of your object
Python 3.13
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
def move(self, dx):
self.x += dx
point = Point(1, 2)
print([name for name in dir(point) if not name.startswith('_')])['move', 'x', 'y']
String methods that start with is
Python 3.13
print([name for name in dir(str) if name.startswith('is')])['isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper']
Details
Names like __init__ are special methods; to see only the regular ones, filter out names with underscores, as in the first example. dir is for exploring an object; in a program, checking an attribute with hasattr is more convenient.
