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

时间:2014-12-03 16:35:37

标签: python

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

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函数的哪种类型的对象,我正在学习如何使用,返回以便我可以转到该对象的doc页面并学习如何使用它,即它的API?

1 个答案:

答案 0 :(得分:1)

与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'>

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

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