2017-08-21 20 views
7

पायथन dir() वर्तमान स्थानीय दायरे में नामों की सूची देता है। __doc__ किसी ऑब्जेक्ट का पूर्ण डॉकस्ट्रिंग देता है।क्या पाइथन ऑब्जेक्ट के सभी सदस्यों के लिए डॉकस्ट्रिंग का एक छोटा संस्करण मुद्रित करने का कोई तरीका है?

मैं वर्तमान स्थानीय दायरे में सभी नामों को कैसे सूचीबद्ध कर सकता हूं और प्रत्येक आइटम के डॉकस्ट्रिंग की पहली पंक्ति मुद्रित कर सकता हूं?

विस्तृत करने के लिए: import numpy as np के लिए मैं dir(np) द्वारा लौटाए गए सभी नामों के संक्षिप्त विवरणों की एक सूची प्राप्त करना चाहता हूं उदा। print(np.nonzero.__doc__.split('.', 1)[0])

मैं यह कैसे कर सकता हूं?

उत्तर

5
def print_members(obj): 
    for key in dir(obj): 
     value = getattr(obj, key) 
     doc = (value.__doc__ or '').split('.', 1)[0] 
     print('MEMBER: %s\nDOCSTRING: %s\n\n' % (key, doc)) 
0

मैंने इस तरह कुछ उपयोग किया है। यह वही है लेकिन वास्तव में नहीं जो आप खोज रहे हैं। आप अपनी जरूरतों के लिए डॉकस्ट्रिंग आउटपुट समायोजित कर सकते हैं।

############################################################################### 
def about(obj=None, text=None, capsOnly=False, noLeadingUnderScores=False): 
    """ 
    Utility function to assist with discovery while playing in the Python 
    shell. When possible, returns the sorted dir() and values() of the 
    specified object. 

    * noLeadingUnderScores - toggles display filtering of items that have a 
          leading underscore; only applies to dir() not 
          values() items. 

    * capsOnly - toggles display filtering of items that are uppercase only. 
       this only applies to dir() not values() items. 

    * text -  toggles the display filtering of items that have 'text' 
       within their string; applies to both dir() and values() items. 
    """ 

    print "\n*******************\n* print __obj__ *\n*******************\n" 
    if obj is None: 
     print "about() is meaningless as 'obj' is None" 
    else: 
     # diplay help(), if possible 
     try: 
      if obj.__doc__: 
       print "\n\n********************\n* HELP() results *\n********************\n" 
       for x in obj.__doc__.split('\n'): 
         print x 
     except: 
      print "\nno __obj__ available" 


     # display dir(), if possible 
     print "\n\n*******************\n* DIR() results *\n*******************\n" 
     for x in sorted(dir(obj)): 
      temp = "%s" % x 
      if noLeadingUnderScores and len(temp) > 0 and temp[0] == "_": 
       continue 
      elif capsOnly: 
       if temp == temp.upper(): 
        if text and text in temp: 
         print temp 
       else: 
        continue 
      elif text: 
       if text in temp: 
        print temp 
      else: 
       print temp 

     # display values(), is possible 
     try: 
      if obj.values and type(obj.values) == type({}): 
       print "\n\n**********************\n* DICT values(k,v) *\n**********************\n" 
       for x in sorted(obj.values.keys()): 
        if text: 
         if text in x or text in str(obj.values[x]): 
          print x, obj.values[x] 
        else: 
         print x, obj.values[x] 
     except: 
      print "\nno dictionary like obj.values available" 
संबंधित मुद्दे

 संबंधित मुद्दे