dir()
returns a list of all the defined names, but it is annoying to try to call function I see listed only to discover it is actually an attribute, or to try to access an attribute only to discover it is actually a callable. How can I get dir()
to be more informative?
The dir() function returns all properties and methods of the specified object, without the values. This function will return all the properties and methods, even built-in properties which are default for all object.
Attributes of a class can also be accessed using the following built-in methods and functions : getattr() – This function is used to access the attribute of object. hasattr() – This function is used to check if an attribute exist or not. setattr() – This function is used to set an attribute.
help() – it is built in function python which when executed, returns docstring along with module name, filename, function name and constant of the module passed as argument. dir()- it is built in function in python which is used to display properties and methods of object passed as an argument in it.
dir() function 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.) Returns : dir() tries to return a valid list of attributes of the object it is called upon.
To show a list of the defined names in a module, for example the math module, and their types you could do:
[(name,type(getattr(math,name))) for name in dir(math)]
getattr(math,name) returns the object (function, or otherwise) from the math module, named by the value of the string in the variable "name". For example type(getattr(math,'pi')) is 'float'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With