Python dir()

In this post, we will see about Python dir() method.
Python dir() method tries to return list of available attributes of the object.





dir returns list of all availables attributes.
It internally calls __dir__ method if available.__dir__ method must return all the attibute available.If __dir__ method is not present, then it tries to get attributes from __dict__ method and from the type object.In this case, you may not get all list of attributes.


Let’s see a very simple example:

dir for inbuild objects

st='Hello' print("List of attributes for string:",dir(st)) l=[1,2,3,4] print("List of attributes for list:",dir(l))


List of attributes for string: [‘__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’, ‘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 of attributes for list: [‘__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’]

dir for user defined objects

Let’s see another example for user defined class.

class Country: def __dir__(self): return ['countryName', 'capitalName', 'population'] india = Country() print("List of attributes for Country:",dir(india))


List of attributes for Country: [‘capitalName’, ‘countryName’, ‘population’]

dir with no arguments

If you call dir without any argument,it will return all the attribute in current scope.

s='Dummy String' l=[1,3,5,7] print("List of attributes in current scope are:",dir())


List of attributes in current scope are: [‘In’, ‘Out’, ‘_’, ‘__’, ‘___’, ‘__builtin__’, ‘__builtins__’, ‘__doc__’, ‘__loader__’, ‘__name__’, ‘__package__’, ‘__spec__’, ‘_dh’, ‘_i’, ‘_i1’, ‘_ih’, ‘_ii’, ‘_iii’, ‘_oh’, ‘exit’, ‘get_ipython’, ‘l’, ‘quit’, ‘s’]

That’s all about Python dir function.

Leave a Reply

Your email address will not be published. Required fields are marked *