Python dir() Method – List Object Attributes

The python programming language is an object-based programming language where every variable, value, etc. is an object. Objects contain different attributes and these attributes are different from the object type. In order to list and display object attributes Python provides the dir() method which will return all attribute names as a list. The dir() method is a built-in method that is provided by default and there is no need to install or import an extra module.

dir() Method Syntax

The dir() method has the following syntax where is accepts single parameter.

  • OBJECT is a variable, value, class etc. which attributes will be returned. This parameter is optional.

The dir() method returns a list of given object attribute names.

List Object Attributes with dir()

In the following example we will list attributes of different object types like list, string, class definition and class instance.

numbers = [1,2,3]

sentence = "I like"

class Person:
   name = "─░smail Baydan"

print("List attributes:\n",dir(numbers))

print("String attributes:\n",dir(sentence))

print("Class attributes:\n",dir(Person))

p = Person()
print("Class instance attributes:\n",dir(p))

The output is like below.

List attributes:
['add', 'class', 'contains', 'delattr', 'delitem', 'dir', 'doc', 'eq', 'format', 'ge', 'getattribute', 'getitem', 'gt', 'hash', 'iadd', 'imul', 'init', 'init_subclass', 'iter', 'le', 'len', 'lt', 'mul', 'ne', 'new', 'reduce', 'reduce_ex', 'repr', 'reversed', 'rmul', 'setattr', 'setitem', 'sizeof', 'str', 'subclasshook', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']

String attributes:
['add', 'class', 'contains', 'delattr', 'dir', 'doc', 'eq', 'format', 'ge', 'getattribute', 'getitem', 'getnewargs', 'gt', 'hash', 'init', 'init_subclass', 'iter', 'le', 'len', 'lt', 'mod', 'mul', 'ne', 'new', 'reduce', 'reduce_ex', 'repr', 'rmod', 'rmul', 'setattr', 'sizeof', 'str', 'subclasshook', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']

Class attributes:
['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', 'name']

Class instance attributes:
['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', 'name']

List of Names In The Current Local Scope

The object parameter for the dir() method is optional. If no object parameter is provided into the dir() method the list of the names in the current local scope will be returned like below. This will include imported modules, variables, functions, lists, classes, etc.


Output is like below.

['Person', 'annotations', 'builtins', 'doc', 'loader', 'name', 'package', 'spec', 'a', 'age', 'count', 'errmessage', 'i', 'items', 'multiplY', 'multiply', 'numbers', 'os', 'p', 'print_err', 'result', 'say_hello', 'sentence', 'sentence_list', 'sys']

Leave a Comment