Manuel de Leon
Manuel de Leon

Reputation: 2603

Does Python have a method that returns all the attributes in a module?

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

Upvotes: 1

Views: 317

Answers (5)

ladyfafa
ladyfafa

Reputation: 7269

dir is what you need :)

Upvotes: 0

aaronasterling
aaronasterling

Reputation: 71014

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]

Upvotes: 8

Escualo
Escualo

Reputation: 42082

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)

Upvotes: 0

Andrew Jaffe
Andrew Jaffe

Reputation: 27087

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.

Upvotes: 0

nmichaels
nmichaels

Reputation: 50971

import module
dir(module)

Upvotes: 4

Related Questions