Movatterモバイル変換


[0]ホーム

URL:


Python Tutorial

Python dir() Function



ThePython dir() function lists out all the properties and methods, including the default properties of the specified object. Here, the object could be any module, function, string, list, dictionary, etc.

If no parameters are passed todir(), it will return the list of names in the current local scope. When a parameter is provided, it returns a list of valid attributes for that object without values.

Thedir() function is one of thebuilt-in functions and does not require any module to import.

Syntax

The following is the syntax for the pythondir() function.

dir(object)

Parameters

Following is the parameter of the pythondir() function −

  • object − This parameter specifies the object whose properties are to be listed.

Return Value

The pythondir() function returns a list of properties of the specified object.

dir() Function Example

Practice the following examples to understand the use ofdir() function in Python:

Example: dir() Function Without Any Argument

When we print the value of the dir() without passing any arguments, we get the list ofmethods andattributes that are available as part of the standard library.

print("dir method without using any arguments")print(dir())

On executing the above program, the following output is generated −

dir method without using any arguments['__annotations__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', 'traceback']

Example: dir() Function With User-defined Class

If we pass an object of user-defined class to the dir() function, it will display the attributes and methods of the object. It also includes those built-in properties which are default for that particular object.

class newClass:   def __init__(self):      self.x = 55      self._y = 44   def newMethod(self):      passnewObj = newClass()print("List of methods and properties of given class:")print(dir(newObj))

The following is the output obtained by executing the above program −

List of methods and properties of given class:['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_y', 'newMethod', 'x']

Example: Print Module's Attributes and Methods Using dir()

Thedir() function allows us to display attributes and methods of a built-in module of Python as demonstrated in the code below.

import mathprint("List of methods and properties of MATH module:")print(dir(math))

The following output is obtained by executing the above program -

List of methods and properties of MATH module:['__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'cbrt', 'ceil', 'comb', 'copysign', 'cos', 'cosh', 'degrees', 'dist', 'e', 'erf', 'erfc', 'exp', 'exp2', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan', 'isqrt', 'lcm', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan', 'nextafter', 'perm', 'pi', 'pow', 'prod', 'radians', 'remainder', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau', 'trunc', 'ulp']

Example: Inspect Inherited Methods and Properties of Parent Class

If we want to inspect inherited methods and properties of parent class, then we can use the dir() function by passing the obejct of the child class.

class Pclass:   def pMethod(self):      passclass Chclass(Pclass):   def chMethod(self):      passchObj = Chclass()print("List of methods and properties of Parent class:")print(dir(chObj))

The above program, on executing, displays the following output −

List of methods and properties of Parent class:['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'chMethod', 'pMethod']
python_built_in_functions.htm
Print Page
Advertisements

[8]ページ先頭

©2009-2025 Movatter.jp