一个示例(列出
optparse.OptionParser类的方法):
>>> from optparse import OptionParser>>> import inspect#python2>>> 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>), ... ]# python3>>> inspect.getmembers(OptionParser, predicate=inspect.isfunction)...请注意,它
getmembers返回2元组的列表。第一项是成员的名称,第二项是值。
您还可以将实例传递给
getmembers:
>>> parser = OptionParser()>>> inspect.getmembers(parser, predicate=inspect.ismethod)...



