如何计算Python中不包括注释和文档字符串的代码行数?

发布于 2025-01-01 16:15:19 字数 1539 浏览 0 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

╰沐子 2025-01-08 16:15:19

将 Python 文档字符串包含在“代码行”计数中可能是正确的。通常,编译器会丢弃注释,但会解析文档字符串:

请参阅 PEP 257 - 文档字符串约定

文档字符串是一个字符串文字,作为第一个语句出现
模块、函数、类或方法定义。这样的文档字符串
成为该对象的 __doc__ 特殊属性。

...

Python 代码中其他地方出现的字符串文字也可能充当
文档。 Python 字节码编译器无法识别它们
并且不能作为运行时对象属性访问..

换句话说,文档字符串以非常真实的方式编译并构成程序的代码。此外,它们通常被 doctest 模块 用于单元测试,如用法命令行实用程序的字符串等。

It is probably correct to include Python docstrings in a "lines of code" count. Normally a comment would be discarded by the compiler, but docstrings are parsed:

See PEP 257 - Docstring Conventions:

A docstring is a string literal that occurs as the first statement in
a module, function, class, or method definition. Such a docstring
becomes the __doc__ special attribute of that object.

...

String literals occurring elsewhere in Python code may also act as
documentation. They are not recognized by the Python bytecode compiler
and are not accessible as runtime object attributes..

In other words, docstrings are compiled and constitute, in a very real way, the code of the program. Additionally, they're commonly used by the doctest module for unit testing, as usage strings for command line utilities, and so on.

一笑百媚生 2025-01-08 16:15:19

注释行可以是 python 中的代码行。例如,请参阅doctest

此外,您将很难找到一种明智/可靠的方法来将这样的情况视为注释或代码:

foo = ('spam', 
       '''eggs
          eggs
          eggs'''
       '''more spam''',
       'spam')

也只需计算注释行,我认为大多数程序员都会同意这对于您来说是一个很好的衡量标准实际上正在尝试测量。

Comment lines can be lines of code in python. See doctest for example.

Moreover, you will have trouble to find a sensible/reliable way to consider a case like this as being a comment or code:

foo = ('spam', 
       '''eggs
          eggs
          eggs'''
       '''more spam''',
       'spam')

Just count the comment lines as well, I think most programmers will agree it is as good a measure for whatever you are actually trying to measure.

三岁铭 2025-01-08 16:15:19

Tahar 不计算文档字符串。这是它的 count_loc 函数:

def count_loc(lines):
    nb_lines  = 0
    docstring = False
    for line in lines:
        line = line.strip()

        if line == "" \
           or line.startswith("#") \
           or docstring and not (line.startswith('"""') or line.startswith("'''"))\
           or (line.startswith("'''") and line.endswith("'''") and len(line) >3)  \
           or (line.startswith('"""') and line.endswith('"""') and len(line) >3) :
            continue

        # this is either a starting or ending docstring
        elif line.startswith('"""') or line.startswith("'''"):
            docstring = not docstring
            continue

        else:
            nb_lines += 1

    return nb_lines

Tahar doesn't count the docstrings. Here's its count_loc function :

def count_loc(lines):
    nb_lines  = 0
    docstring = False
    for line in lines:
        line = line.strip()

        if line == "" \
           or line.startswith("#") \
           or docstring and not (line.startswith('"""') or line.startswith("'''"))\
           or (line.startswith("'''") and line.endswith("'''") and len(line) >3)  \
           or (line.startswith('"""') and line.endswith('"""') and len(line) >3) :
            continue

        # this is either a starting or ending docstring
        elif line.startswith('"""') or line.startswith("'''"):
            docstring = not docstring
            continue

        else:
            nb_lines += 1

    return nb_lines
笑叹一世浮沉 2025-01-08 16:15:19

你有没有看过http://www.ohloh.net/p/ohcount - 总是很漂亮对我来说钱 - 虽然我不使用 python

Have you looked at http://www.ohloh.net/p/ohcount - always been pretty on the money for me - although I do not use python

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文