If you want to get list of methods in a Python class? An example (listing the methods of the optparse.OptionParser class):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | >>> from optparse import OptionParser >>> import inspect >>> inspect.getmembers(OptionParser, predicate=inspect.ismethod) [([('__init__', <unbound method OptionParser.__init__>), ... ('add_option', <unbound method OptionParser.add_option>), ('add_option_group', <unbound method OptionParser.add_option_group>), ('add_options', <unbound method OptionParser.add_options>), ('check_values', <unbound method OptionParser.check_values>), ('destroy', <unbound method OptionParser.destroy>), ('disable_interspersed_args', <unbound method OptionParser.disable_interspersed_args>), ('enable_interspersed_args', <unbound method OptionParser.enable_interspersed_args>), ('error', <unbound method OptionParser.error>), ('exit', <unbound method OptionParser.exit>), ('expand_prog_name', <unbound method OptionParser.expand_prog_name>), ... ] |
Notice that getmembers returns a list of 2-tuples. The first item is the name of the member, the second item is the value.
1 2 | >>> parser = OptionParser() >>> inspect.getmembers(parser, predicate=inspect.ismethod) |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.