Python dir() Function

Returns a list of the specified object's properties and methods. If no object is specified, dir() returns the list of names in the current local scope. The dir() function is provided primarily as a convenience for use at an interactive prompt, for example, asking what function the user would like to use.

Contrast dir() the vars() function, which does not include class attributes and the attributes of super classes.

Syntax

Python
dir(object)

Parameters

ParameterDescription
object Required. The object whose properties and methods are to be listed.

Example

Python
class UserFunctions:
    q = 5

    def add(x, y):
        return x + y

    def subtract(x, y):
        return x - y

# All names in the class UserFunctions    
print('All methods and properties of UserFunctions:')
print(dir(UserFunctions))

Output

All methods and properties of UserFunctions:
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'add', 'q', 'subtract']

Notes

It may be more useful to exclude the built-in names so you get just the class methods and properties. The following example uses a lambda expression to filter all strings that do not begin with '__':

Example:
Python
class UserFunctions:
    q = 5

    def add(x, y):
        return x + y

    def subtract(x, y):
        return x - y

print('\nMethods and properties that are not built-ins:')
functions = list(filter(lambda prop : not(prop.startswith('__')), dir(UserFunctions)))
print(functions)
Output:
Methods and properties that are not built-ins:
['add', 'q', 'subtract']