Saturday 5 December 2015

Python: get names of module

'dir' function is used to find out all the functions that a module defines.

For example, ‘arithmetic.py’ is defined like below.

arithemetic.py
def sum(a, b):
  return a+b

def subtract(a, b):
  return a-b
  
def mul(a,b):
  return a*b
  
def div(a, b):
  return a/b

>>> import arithmetic
>>> dir(arithmetic)
['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'div', 'mul', 'subtract', 'sum']
>>> 

Without arguments, dir() lists the names you have defined currently:
>>> dir()
['__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', 'arithmetic']
>>> 
>>> import sys
>>> dir()
['__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', 'arithmetic', 'sys']





Previous                                                 Next                                                 Home

No comments:

Post a Comment