0

我正在部署用 python (Flask) 编写的简单 API,但我收到奇怪的错误“TypeError: 'float' object is not callable”。但是,对象是 int 类型,我证实了这一点。我什至尝试通过“for counter in range(int(additional_params_count)):”来强制执行 int 类型,但它没有帮助。

以下是错误信息和来源

 1
 <type 'int'>
 [20130826-16:14PM] [flats_api] [ERROR] Exception on /v1/closest_point [GET]
 Traceback (most recent call last):
   File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1817, in wsgi_app
     response = self.full_dispatch_request()
   File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1477, in full_dispatch_request
     rv = self.handle_user_exception(e)
   File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1381, in handle_user_exception
     reraise(exc_type, exc_value, tb)
   File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1475, in full_dispatch_request
     rv = self.dispatch_request()
   File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1461, in dispatch_request
     return self.view_functions[rule.endpoint](**req.view_args)
   File "/var/www/flats_api/flats_api.py", line 104, in closest_point
     for counter in range(0,additional_params_count):
 TypeError: 'float' object is not callable

来源:

def closest_point():
         lng = request.args.get('lng', default=None, type=float)
         lat = request.args.get('lat', default=None, type=float)

         price_min = request.args.get('price_min',default=None, type=float)
         price_max = request.args.get('price_max',default=None, type=float)

         rooms_min = request.args.get('rooms_min',default=None, type=float)
         rooms_max = request.args.get('rooms_max',default=None, type=float)

         if (lng != None) and (lat != None):
                 range = 0.1
                 lng_min = lng - range
                 lng_max = lng + range
                 lat_min = lat - range
                 lat_max = lat + range

                 cur = g.db.cursor()
                 query = "SELECT id, link, price, longitude, latitude FROM " + DATABASE_TABLE_NAME + " WHERE latitude >= " + str(lat_min) + " AND latitude <= " + str(lat_max) + " AND longitude >= " + str(lng_min) + " AND longitude <= " + str(lng_max)

                 # I always have 5 basic parameters to group by. I count number of additional parameters to know how many to add
                 additional_params_count = 0
                 if price_min:
                         query = query + " AND price >= " + str(price_min)
                         additional_params_count = additional_params_count + 1
                 query = query + "GROUP BY 1,2,3,4,5"
                 print additional_params_count
                 print type(additional_params_count)
                 for counter in range(0,additional_params_count):
                         print counter
                         query = query + "," + str(counter + 6)
                 query = query + ";"
                 print query
                 cur.execute(query)
                 columns = [desc[0] for desc in cur.description]
                 rows = cur.fetchall()
                 flats_json = []
                 for row in rows:
                         flats_json.append(dict((k,str(v)) for k,v in zip(columns,row)))
                 response_body = str(rows)
                 return json.dumps(flats_json)
         else:
                 return "One of lng or lat parameters missing"
4

1 回答 1

3

您在代码中分配range了一个浮点数,请不要这样做,否则您将无法访问range()内置函数:

if (lng != None) and (lat != None):
     range = 0.1       ###########

演示:

>>> range = 0.1
>>> range()
Traceback (most recent call last):
  File "<ipython-input-39-7b0c968826c1>", line 1, in <module>
    range()
TypeError: 'float' object is not callable
于 2013-08-26T14:24:57.193 回答