【问题标题】:Python details of any method [duplicate]任何方法的Python详细信息[重复]
【发布时间】:2016-09-07 20:54:13
【问题描述】:

是否可以查看方法究竟是如何工作的?例如列表方法 count()。 我有一个清单:

li = [3, 5, 6, 7, 3, 3, 3]

当我输入时

print li.count(3)

输出将是 4。我怎样才能看到发生这种魔法的代码? 命令 help(list.count) 给出的信息不足:

>>> help(list.count)
Help on method_descriptor:

count(...)
    L.count(value) -> integer -- return number of occurrences of value

【问题讨论】:

  • help(list.count),阅读documentation 或查看the source
  • 你试过用谷歌搜索 Python 的源代码吗?
  • 是的,我试图在 Google 中找到此代码。但我没有成功。

标签: python


【解决方案1】:

大多数内置函数都是用 C 实现的,因此您将看不到代码。但是,您可以通过“帮助”功能获得对所有内容的详细帮助。

help(li.count)

这为您提供了足够的信息,让您真正了解您可以对您提供给帮助的任何对象执行哪些操作。我刚开始时所做的是编写自己的函数来模拟该功能。这使您可以很好地掌握您必须考虑的所有事情。 下面是一个计数函数的示例:

def count(crit, iterable):
    i = 0
    for item in iterable:
        if crit == item:
            i += 1
    return i

作为替代方案,很多东西(例如 tkinter 模块)是用 Python 编写的,您可以在 pythonx.x/Lib/tkinter 中查找它们(将 tkinter 替换为您想要查看的任何模块)。 我希望这能很好地回答你的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-07
    • 2013-01-17
    • 2014-04-13
    • 2021-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多