Skip to main content
 首页 » 编程设计

python之在标准 Python 文档中,如何找到函数返回的对象类型

2025年05月04日13sky-heaven

考虑 this Python 文档页面。如果向下滚动到功能目录,您可以看到如下描述:

xmlparser.Parse(data[, isfinal]) 
 
    Parses the contents of the string data, calling the appropriate handler functions to process the parsed data. isfinal must be true on the final call to this method; it allows the parsing of a single file in fragments, not the submission of multiple files. data can be the empty string at any time. 

不同于 typical format of Javadoc ,本文档格式没有指定函数返回对象的类型(字符串、数字、列表、字典或它自己定义的类型等)。

我如何找出我刚刚学习如何使用的 Python 函数返回的对象类型,以便我可以转到该对象的文档页面并学习如何使用它,即它的 API?

请您参考如下方法:

与 Java 不同,python 函数可以返回多种类型。

例如:

def test(v): 
    if v == 0 : 
        return 1 
    if v == 1 : 
        return "Foo" 
    if v == 2 :  
        return [] 

是一个有效的函数,并且

for i in xrange(4): 
    print type(test(i)) 

将返回:

<type 'int'> 
<type 'str'> 
<type 'list'> 
<type 'NoneType'> 

据我所知,阅读文档或尝试函数并使用 type() 是找出 python 函数返回的对象类型的唯一方法。

在我看来(我没有尝试这个模块),xmlparser.Parse(data) 分析'数据'并调用正确的处理函数,可以由你自己设置,然后什么都不返回(NoneType)。