-2

Sometimes, I need to use dir(obj) to find which members and methods belong to obj. Then I got a lot of methods, i.e. a big list like the following:

['__class__', '__delattr__', '__delitem__', '__dict__', '__doc__', '__eq__', '__format__', '__getattribute__', '__hash__', '__init__', '__iter__', '__javaclass__', '__javaobject__', '__len__', '__metaclass__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_addAll', '_removeAll', '_retainAll', 'add', 'addAll', 'addChild', 'ancestor', 'cCommands', 'children', 'childrenAsList', 'clear', 'constituents', 'contains', 'containsAll', 'daughterTrees', 'deepCopy', 'deeperCopy', 'dependencies', 'depth', 'dominates', 'dominationPath', 'equals', 'factory', 'firstChild', 'flatten', 'getChild', 'getChildrenAsList', 'getClass', 'getLeaves', 'getNodeNumber', 'getSpan', 'hashCode', 'headPreTerminal', 'headTerminal', 'indentedListPrint', 'indexLeaves', 'indexOf', 'indexSpans', 'insertDtr', 'isEmpty', 'isLeaf', 'isPhrasal', 'isPrePreTerminal', 'isPreTerminal', 'isUnaryRewrite', 'iterator', 'joinNode', 'label', 'labelFactory', 'labeledYield', 'labels', 'lastChild', 'leaves', 'leftCharEdge', 'localTree', 'localTrees', 'mapDependencies', 'nodeNumber', 'nodeString', 'notify', 'notifyAll', 'numChildren', 'parent', 'pathNodeToNode', 'pennPrint', 'pennString', 'percolateHeads', 'postOrderNodeList', 'preOrderNodeList', 'preTerminalYield', 'printLocalTree', 'prune', 'remove', 'removeAll', 'removeChild', 'retainAll', 'rightCharEdge', 'score', 'setChild', 'setChildren', 'setFromString', 'setLabel', 'setLabels', 'setScore', 'setSpans', 'setValue', 'siblings', 'size', 'skipRoot', 'span', 'spliceOut', 'subTreeList', 'subTrees', 'taggedDependencies', 'taggedYield', 'toArray', 'toString', 'toStringBuilder', 'toStructureDebugString', 'transform', 'treeFactory', 'upperMostUnary', 'value', 'valueOf', 'wait', 'yield_']

Could anyone tell me how to search these results, if I want to find all methods name contains 'value'?

jamylak
  • 128,818
  • 30
  • 231
  • 230
Xing Shi
  • 2,152
  • 3
  • 21
  • 32

2 Answers2

4
[x for x in dir(obj) if 'value' in x]
jamylak
  • 128,818
  • 30
  • 231
  • 230
0

The following statement will test if the object has the attribute 'value':

if hasattr(obj, 'value'):

where obj is the object whose attributes you are searching in.

jamylak
  • 128,818
  • 30
  • 231
  • 230
Wedava
  • 1,211
  • 2
  • 16
  • 30