Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

dir()

Lists names that an object advertises for interactive discovery.

Version: Python 3.14; repository image, production runtime not attested

Syntax

dir()
dir(object, /)

Lists names that an object advertises for interactive discovery.

A class can customize directory results, so the list need not contain every accessible attribute.

The result is a sorted list of names useful for interactive inspection. An object can customize it with __dir__. Here only shown is listed, yet value is still accessible as 7, and the advertised name need not exist. With no argument, dir gives names in the current local scope.

class Route:
    value = 7
    def __dir__(self): return ["z", "a"]
r = Route()
assert dir(r) == ["a", "z"] and r.value == 7

Examples

class Route:
    value = 7
    def __dir__(self):
        return ["shown"]

r = Route()
print(dir(r))
print(r.value)

Result

['shown']
7

The result is a sorted list of names useful for interactive inspection. An object can customize it with `__dir__`. Here only shown is listed, yet value is still accessible as 7, and the advertised name need not exist. With no argument, dir gives names in the current local scope.

Sources