amphibient
amphibient

Reputation: 31230

In standard Python doc, how do you find which object type a function returns?

Consider this Python doc page. If you scroll down to the function catalog, you can see descriptions such as:

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.

Unlike the typical format of Javadoc, this documentation format does not specify the type of the object returned by the function (string, numerical, list, dict, or its own defined type etc.).

How do I find out what type of object a Python function, which I am just learning how to use, returns so I can go to that object's doc page and learn how to use it, i.e. its API ?

Upvotes: 2

Views: 153

Answers (1)

Corentin Limier
Corentin Limier

Reputation: 5006

Unlike Java, a python function can return multiple types.

For example :

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

is a valid function, and

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

will return :

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

Reading the doc or trying the function and using type() is, to the best of my knowledge, the only ways to find out the tyoe of object returned by a python function.

In my opinion (i didn't try this module), xmlparser.Parse(data) analyzes 'data' and calls the right handler functions, which can be set by yourself, and then returns nothing (NoneType).

Upvotes: 1

Related Questions