Python dir() function
Python dir() function The dir() function in Python returns all properties and methods of the specified object, without the values. Syntax
dir([object])Parameter object: This parameter represents the object one wants to see the valid attributes. Return This function will return all the properties and methods, even built-in properties which are default for all objects. Example 1
# Python Program explaining # dir() function print(dir()) # importing two modules import random import math # return the module names added to # the local namespace print(dir())Output
['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__'] ['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'math', 'random']Example 2
# Python Program explaining # dir() function # import the random module import math # when a module Object is passed as a parameter. # attributes in math function print("The contents of the math library are as follows:\n") # module Object is passed as parameter print(dir(math))Output
The contents of the math library are as follows: ['__doc__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'hypot', 'isfinite', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc']