views:

107

answers:

5

I already search for it on Google but I didn't have lucky.

+4  A: 
import module
dir(module)
Nathon
A: 

You're looking for dir:

import os
dir(os)

??dir

    dir([object]) -> list of strings

If called without an argument, return the names in the current scope.
Else, return an alphabetized list of names comprising (some of) the attributes
of the given object, and of attributes reachable from it.
If the object supplies a method named __dir__, it will be used; otherwise
the default dir() logic is used and returns:
  for a module object: the module's attributes.
  for a class object:  its attributes, and recursively the attributes
    of its bases.
  for any other object: its attributes, its class's attributes, and
    recursively the attributes of its class's base classes.
Andrew Jaffe
A: 

As it has been correctly pointed out, the dir function will return a list with all the available methods in a given object.

If you call dir() from the command prompt, it will respond with the methods available upon start. If you call:

import module
print dir(module)

it will print a list with all the available methods in module module. Most times you are interested only in public methods (those that you are supposed to be using) - by convention, Python private methods and variables start with __, so what I do is the following:

import module
for method in dir(module):
 if not method.startswith('_'):
   print method

That way you only print public methods (to be sure - the _ is only a convention and many module authors may fail to follow the convention)

Arrieta
+7  A: 

in addition to the dir builtin that has been mentioned, there is the inspect module which has a really nice getmembers method. Combined with pprint.pprint you have a powerful combo

from pprint import pprint
from inspect import getmembers
import linecache

pprint(getmembers(linecache))

some sample output:

 ('__file__', '/usr/lib/python2.6/linecache.pyc'),
 ('__name__', 'linecache'),
 ('__package__', None),
 ('cache', {}),
 ('checkcache', <function checkcache at 0xb77a7294>),
 ('clearcache', <function clearcache at 0xb77a7224>),
 ('getline', <function getline at 0xb77a71ec>),
 ('getlines', <function getlines at 0xb77a725c>),
 ('os', <module 'os' from '/usr/lib/python2.6/os.pyc'>),
 ('sys', <module 'sys' (built-in)>),
 ('updatecache', <function updatecache at 0xb77a72cc>)

note that unlike dir you get to see that actual values of the members. You can apply filters to getmembers that are similar to the onese that you can apply to dir, they can just be more powerful. For example,

def get_with_attribute(mod, attribute, public=True):
    items = getmembers(mod)
    if public:
       items = filter(lambda item: item[0].startswith('_'), items)
    return [attr for attr, value in items if hasattr(value, attribute]
aaronasterling
A: 

dir is what you need :)

ladyfafa