dir()

dir([object]) returns a list of attributes and methods

object : ( Optional ) List of attributes and methods are returned.

List of names in local scope or attributes and methods of an object. If we input any module then it gives information about the module.
If input object is given then the attributes and methods are returned.

Without any arguments, dir() returns the list of names in current local scope.
Don’t forget to restart and clear the output of the Kernel when you try dir() by adding or removing modules or objects.
print(dir())
Output
['In', 'Out', '_', '_2', '__', '___', '__builtin__', '__builtins__', 
'__doc__', '__loader__', '__name__', '__package__', '__spec__', '_dh',
 '_i', '_i1','_i2', '_i3', '_ih', '_ii', '_iii', '_oh', 'exit', 
 'get_ipython', 'quit']
Using a module

Using object ( user defined )

We get a list of attributes ( without value ). If __dir__() method is there then internally it is called.
class my_class():
    def __dir__(self):
        return ['One','Two','Three']
dir(my_class()) 
Output
['One', 'Three', 'Two']
If __dir__() is not there then __dict__ is used. ( __dict__ is the dictionary containing the object's symbol table)
class my_class():
    def my_name(self):
        self.name='plus2net'
        self.age=20
        return self.name
my=my_class()
#print(my.my_name())
print(dir(my_class())) 
#print(my.__dict__)

Using a list

my_list=['One','Two','Three']
print(dir(my_list))
Output
['__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']

Using module

Let us check math module
import math
print(dir(math))
Output
['__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', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan', 'pi', 'pow', 'radians', 'remainder', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau', 'trunc']

Built in Functions in Python

Here is a list of Built in functions available in core Python. No need to add any external module to use these built in functions. Check here how such list of built in functions is generated by importing builtins module and then using dir() to get the list of functions.
import builtins
for i in dir(builtins):
    #print("<tr><td>" + i + "</td><td> </td></tr>")
    print(i)
print("Total Builtin Functions : ",len(dir(builtins)))

All Built in Functions in Python locals() ord()
Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    Post your comments , suggestion , error , requirements etc here





    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer