Python dir() Method

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 it accepts a single parameter.

dir(OBJECT)
  • OBJECT is a variable, value, class, etc. whose 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]

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

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']

List String Object

The string type provides lots of methods and attributes. These methods and attributes can be listed with dir() method like below.

sentence = "I like pythontect.com"

print("String attributes:\n",dir(sentence))
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']

List Class Object

The class type provides lots of methods and attributes. These methods and attributes can be listed with dir() method like below.

class Person:
   name = "─░smail Baydan"
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']

List Class Instance Object

The class instance type provides lots of methods and attributes. These methods and attributes can be listed with dir() method like below. We can see that the “name” attribute is listed at the end of the class instance attributes.

class Person:
   name = "─░smail Baydan"

p = Person()

print("Class instance attributes:\n",dir(p))
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 in 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.

dir()

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