On Wed, 27 Apr 2005 10:45:05 +0100, Michael Hoffman <[EMAIL PROTECTED]> wrote:
>Mayer wrote: > >> 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 >>> class Foo(list): ... 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: >>> vars(Foo).keys() ['__module__', 'mrev', 'msor', '__dict__', '__weakref__', '__doc__'] Using dir(): >>> dir(Foo) ['__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(Foo) 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[i]=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 -- http://mail.python.org/mailman/listinfo/python-list