(object)
Get lines of comments immediately preceding an object's source code.

    Returns None when source can't be found.
Get lines of comments immediately preceding an object's source code.
Get lines of comments immediately preceding an object s source code .
def getcomments(object):
    """Get lines of comments immediately preceding an object's source code.

    Returns None when source can't be found.
    """
    try:
        lines, lnum = findsource(object)
    except (OSError, TypeError):
        return None

    if ismodule(object):
        # Look for a comment block at the top of the file.
        start = 0
        if lines and lines[0][:2] == '#!': start = 1
        while start < len(lines) and lines[start].strip() in ('', '#'):
            start = start + 1
        if start < len(lines) and lines[start][:1] == '#':
            comments = []
            end = start
            while end < len(lines) and lines[end][:1] == '#':
                comments.append(lines[end].expandtabs())
                end = end + 1
            return ''.join(comments)

    # Look for a preceding block of comments at the same indentation.
    elif lnum > 0:
        indent = indentsize(lines[lnum])
        end = lnum - 1
        if end >= 0 and lines[end].lstrip()[:1] == '#' and \
            indentsize(lines[end]) == indent:
            comments = [lines[end].expandtabs().lstrip()]
            if end > 0:
                end = end - 1
                comment = lines[end].expandtabs().lstrip()
                while comment[:1] == '#' and indentsize(lines[end]) == indent:
                    comments[:0] = [comment]
                    end = end - 1
                    if end < 0: break
                    comment = lines[end].expandtabs().lstrip()
            while comments and comments[0].strip() == '#':
                comments[:1] = []
            while comments and comments[-1].strip() == '#':
                comments[-1:] = []
            return ''.join(comments)
def getcomments ( object ) : try : lines , lnum = findsource ( object ) except ( OSError , TypeError ) : return None if ismodule ( object ) : # Look for a comment block at the top of the file. start = 0 if lines and lines [ 0 ] [ : 2 ] == '#!' : start = 1 while start < len ( lines ) and lines [ start ] . strip ( ) in ( '' , '#' ) : start = start + 1 if start < len ( lines ) and lines [ start ] [ : 1 ] == '#' : comments = [ ] end = start while end < len ( lines ) and lines [ end ] [ : 1 ] == '#' : comments . append ( lines [ end ] . expandtabs ( ) ) end = end + 1 return '' . join ( comments ) # Look for a preceding block of comments at the same indentation. elif lnum > 0 : indent = indentsize ( lines [ lnum ] ) end = lnum - 1 if end >= 0 and lines [ end ] . lstrip ( ) [ : 1 ] == '#' and indentsize ( lines [ end ] ) == indent : comments = [ lines [ end ] . expandtabs ( ) . lstrip ( ) ] if end > 0 : end = end - 1 comment = lines [ end ] . expandtabs ( ) . lstrip ( ) while comment [ : 1 ] == '#' and indentsize ( lines [ end ] ) == indent : comments [ : 0 ] = [ comment ] end = end - 1 if end < 0 : break comment = lines [ end ] . expandtabs ( ) . lstrip ( ) while comments and comments [ 0 ] . strip ( ) == '#' : comments [ : 1 ] = [ ] while comments and comments [ - 1 ] . strip ( ) == '#' : comments [ - 1 : ] = [ ] return '' . join ( comments )