the function of dir() in python
dir() is a powerful inbuilt function in Python3, which returns list of the attributes and methods of any object (say functions , modules, strings, lists, dictionaries etc.)
Syntax :
dir({object})
Parameters :
object [optional] : Takes object name
Python dir() Method Parameters
dir() takes only a single parameter.
object – the dir() function attempts to return all attributes of a specific object. It is optional.
Let check some example on the usage of dir() function.
Example 1: How dir() works?
Output will be as follow:
Example 2: How to use dir() on User-defined Objects.
The output will be as follows.
Rules of dir() function
dir() function tries to return a list of valid attributes of the object.
If the object has __dir__() method, the method will be called and must return the list of attributes.
If the object doesn’t have __dir__() method, this method tries to find information from the __dict__ attribute (if defined) and type object. In this case, the list returned from dir() may not be complete.
If an object is not passed to the dir() method, it returns the list of names in the current local scope.