names of methods, exported functions

M

Mayer

Hello:

Is there a way to see at the python prompt the names of all the public
methods of a class or the names exported by a module? I know that
GUI-based IDEs have a nifty way of displaying these in the form of a
drop-down list, but I'm looking for some function or method that will
simply return a list of strings.

Thanks,

Mayer
 
M

Michael Hoffman

Mayer said:
Is there a way to see at the python prompt the names of all the public
methods of a class or the names exported by a module? I know that
GUI-based IDEs have a nifty way of displaying these in the form of a
drop-down list, but I'm looking for some function or method that will
simply return a list of strings.

Modules generally do not export names[1]. You import a module and
perhaps specific names within the module. The module can specify which
names are "public" by the use of the underscore naming convention and
the magic __all__ name, which can contain a list of those names which
will be imported by default through "from module import *."

That said, dir() is the function you are looking for. If you want to
restrict to only methods on the class, and not just all attributes,
you'll have to check the type of each attribute.
 
V

Ville Vainio

Mayer> Hello:

Mayer> Is there a way to see at the python prompt the names of all
Mayer> the public methods of a class or the names exported by a
Mayer> module? I know that

If you use ipython, you can press <tab> after the period, e.g.

[~]|128> import re
[~]|129> re.<TAB>
re.DOTALL re.MULTILINE re.__all__ re.error re.search
re.I re.S re.__doc__ re.escape re.split
re.IGNORECASE re.U re.__file__ re.findall re.sub
re.L re.UNICODE re.__name__ re.finditer re.subn
re.LOCALE re.VERBOSE re.compile re.match re.template
re.M re.X re.engine re.purge re.__class__
[~]|129> re.

ISTR the completion can be added to plain old python prompt as well,
through rlcompleter.
 
B

Bengt Richter

Mayer said:
Is there a way to see at the python prompt the names of all the public
methods of a class or the names exported by a module? I know that
GUI-based IDEs have a nifty way of displaying these in the form of a
drop-down list, but I'm looking for some function or method that will
simply return a list of strings.

Modules generally do not export names[1]. You import a module and
perhaps specific names within the module. The module can specify which
names are "public" by the use of the underscore naming convention and
the magic __all__ name, which can contain a list of those names which
will be imported by default through "from module import *."

That said, dir() is the function you are looking for. If you want to
restrict to only methods on the class, and not just all attributes,
you'll have to check the type of each attribute.

A few more hints for the OP:

Don't forget that dir(thing) gets you all the inherited methods of thing as well.
To limit it to the attribute names of thing, use vars(thing).keys(). If thing
is a class, that will show the methods. If thing is an instance, it will show
instance attribute names, so if you want the immediate class methods, use
vars(type(thing)).keys(). If you want gobs of info use help(thing).
E.g., if you have your own class Foo like
... def mrev(self): return Foo(reversed(self))
... def msor(self): return sorted(self)
...
>>> foo = Foo('zvbac')
>>> foo ['z', 'v', 'b', 'a', 'c']
>>> foo.mrev() ['c', 'a', 'b', 'v', 'z']
>>> foo.msor()
['a', 'b', 'c', 'v', 'z']


The short form:
['__module__', 'mrev', 'msor', '__dict__', '__weakref__', '__doc__']

Using dir():
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__dict__
', '__doc__', '__eq__', '__ge__', '__getattribute__', '__getitem__', '__getslice__', '__gt__', '
__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mod
ule__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__
', '__rmul__', '__setattr__', '__setitem__', '__setslice__', '__str__', '__weakref__', 'append',
'count', 'extend', 'index', 'insert', 'mrev', 'msor', 'pop', 'remove', 'reverse', 'sort']

Using help: (BTW, it would be nice to have some keyword arguments for help to filter down
information to exclude what may not be interesting)
Help on class Foo in module __main__:

class Foo(__builtin__.list)
| Method resolution order:
| Foo
| __builtin__.list
| __builtin__.object
|
| Methods defined here:
|
| mrev(self)
|
| msor(self)
|
| ----------------------------------------------------------------------
| Data and other attributes defined here:
|
| __dict__ = <dictproxy object>
| dictionary for instance variables (if defined)
|
| __weakref__ = <attribute '__weakref__' of 'Foo' objects>
| list of weak references to the object (if defined)
|
| ----------------------------------------------------------------------
| Methods inherited from __builtin__.list:
|
| __add__(...)
| x.__add__(y) <==> x+y
|
| __contains__(...)
| x.__contains__(y) <==> y in x
|
| __delitem__(...)
| x.__delitem__(y) <==> del x[y]
|
| __delslice__(...)
| x.__delslice__(i, j) <==> del x[i:j]
|
| Use of negative indices is not supported.
|
| __eq__(...)
| x.__eq__(y) <==> x==y
|
| __ge__(...)
| x.__ge__(y) <==> x>=y
|
| __getattribute__(...)
| x.__getattribute__('name') <==> x.name
|
| __getitem__(...)
| x.__getitem__(y) <==> x[y]
|
| __getslice__(...)
| x.__getslice__(i, j) <==> x[i:j]
|
| Use of negative indices is not supported.
|
| __gt__(...)
| x.__gt__(y) <==> x>y
|
| __hash__(...)
| x.__hash__() <==> hash(x)
|
| __iadd__(...)
| x.__iadd__(y) <==> x+=y
|
| __imul__(...)
| x.__imul__(y) <==> x*=y
|
| __init__(...)
| x.__init__(...) initializes x; see x.__class__.__doc__ for signature
|
| __iter__(...)
| x.__iter__() <==> iter(x)
|
| __le__(...)
| x.__le__(y) <==> x<=y
|
| __len__(...)
| x.__len__() <==> len(x)
|
| __lt__(...)
| x.__lt__(y) <==> x<y
|
| __mul__(...)
| x.__mul__(n) <==> x*n
|
| __ne__(...)
| x.__ne__(y) <==> x!=y
|
| __repr__(...)
| x.__repr__() <==> repr(x)
|
| __reversed__(...)
| L.__reversed__() -- return a reverse iterator over the list
|
| __rmul__(...)
| x.__rmul__(n) <==> n*x
|
| __setitem__(...)
| x.__setitem__(i, y) <==> x=y
|
| __setslice__(...)
| x.__setslice__(i, j, y) <==> x[i:j]=y
|
| Use of negative indices is not supported.
|
| append(...)
| L.append(object) -- append object to end
|
| count(...)
| L.count(value) -> integer -- return number of occurrences of value
|
| extend(...)
| L.extend(iterable) -- extend list by appending elements from the iterable
|
| index(...)
| L.index(value, [start, [stop]]) -> integer -- return first index of value
|
| insert(...)
| L.insert(index, object) -- insert object before index
|
| pop(...)
| L.pop([index]) -> item -- remove and return item at index (default last)
|
| remove(...)
| L.remove(value) -- remove first occurrence of value
|
| reverse(...)
| L.reverse() -- reverse *IN PLACE*
|
| sort(...)
| L.sort(cmp=None, key=None, reverse=False) -- stable sort *IN PLACE*;
| cmp(x, y) -> -1, 0, 1
|
| ----------------------------------------------------------------------
| Data and other attributes inherited from __builtin__.list:
|
| __new__ = <built-in method __new__ of type object>
| T.__new__(S, ...) -> a new object with type S, a subtype of T


Regards,
Bengt Richter
 
M

Michael Hoffman

Bengt said:
Using help: (BTW, it would be nice to have some keyword arguments for help to filter down
information to exclude what may not be interesting)

No kidding. What do you think the output should look like in that case?

I think we don't need the mro, or any of the magic methods inherited
from the builtin.
 
M

M.E.Farmer

Michael said:
No kidding. What do you think the output should look like in that case?

I think we don't need the mro, or any of the magic methods inherited
from the builtin.

The help code is part of pydoc, IIRC, feel free to have a go at it ;)
M.E.Farmer
 
M

M.E.Farmer

Appears that most of the relevant code is in the Helper and TextDoc
classes of pydoc also might need the doc() function.
My head hurts every time I stare at the internals of pydoc ;)
I guess it is time for a lunch break.
M.E.Farmer
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top