-1

我有一个返回数字的函数。我将此数字添加到整数类型,但它给我一个错误,说函数返回的数字是“函数”类型。

如何将 'function' 类型转换为 'int' 类型,以便对它们执行数学函数?有没有办法让函数返回一个'int'?我在函数中尝试了以下内容,但它仍然不会返回“int”类型:

def cost_of_trip(city, days, extra):
    return int(car_cost(days) + hotel(days) + transportation(city) + extra)

print cost_of_trip("Madrid", 7, 2500)

print type(cost_of_trip)

但函数仍然返回:

type 'function'
4

1 回答 1

5

函数对象本身的类型将始终是function(它还能是什么?),但其结果的类型是 an int(如果你让它返回一个):

>>> def foo(bar):
...     return int(bar)
...
>>> type(foo)
<type 'function'>
>>> type(foo("1"))
<type 'int'>
>>>
于 2013-09-19T15:08:50.090 回答