AttributeError: 'function' object has no attribute 'response'
AttributeError: 'function' object has no attribute 'response'
我在主脚本中使用了一个错误处理模块来向 API 发出请求。我想 return "response" 和 "data" 在主脚本中使用。它一直工作到尝试打印 "response"。为不一致道歉,我显然还在学习。如果不先犯一些错误,我就不会学习。我感谢建设性的批评。
my_module
import requests
import json
def errorHandler(url):
try:
response = requests.get(url, timeout=5)
status = response.status_code
data = response.json()
except requests.exceptions.Timeout:
print "Timeout error.\n"
except requests.exceptions.ConnectionError:
print "Connection error.\n"
except ValueError:
print "ValueError: No JSON object could be decoded.\n"
else:
if response.status_code == 200:
print "Status: 200 OK \n"
elif response.status_code == 400:
print "Status: " + str(status) + " error. Bad request."
print "Correlation ID: " + str(data['correlationId']) + "\n"
else:
print "Status: " + str(status) + " error.\n"
return response
return data
my_script
errorHandler("https://api.weather.gov/alerts/active")
print "Content type is " + response.headers['content-type'] +".\n" #expect geo+json
# I need the data from the module to do this, but not for each get request
nwsId = data['features'][0]['properties']['id']
错误
Traceback (most recent call last):
File "my_script.py", line 20, in <module>
print errorHandler.response
AttributeError: 'function' object has no attribute 'response'
如果你想 return 多个值,你 return 它们作为单个语句中的元组:
return response, data
然后在调用者中,您将它们分配给具有元组分配的变量:
response, data = errorHandler("https://api.weather.gov/alerts/active")
print "Content type is " + response.headers['content-type'] +".\n"
nwsId = data['features'][0]['properties']['id']
但是,如果发生任何异常,您的函数将无法正常工作。如果出现异常,它不会设置变量 response
或 data
,因此当它尝试 return 时会出现错误。
我在主脚本中使用了一个错误处理模块来向 API 发出请求。我想 return "response" 和 "data" 在主脚本中使用。它一直工作到尝试打印 "response"。为不一致道歉,我显然还在学习。如果不先犯一些错误,我就不会学习。我感谢建设性的批评。
my_module
import requests
import json
def errorHandler(url):
try:
response = requests.get(url, timeout=5)
status = response.status_code
data = response.json()
except requests.exceptions.Timeout:
print "Timeout error.\n"
except requests.exceptions.ConnectionError:
print "Connection error.\n"
except ValueError:
print "ValueError: No JSON object could be decoded.\n"
else:
if response.status_code == 200:
print "Status: 200 OK \n"
elif response.status_code == 400:
print "Status: " + str(status) + " error. Bad request."
print "Correlation ID: " + str(data['correlationId']) + "\n"
else:
print "Status: " + str(status) + " error.\n"
return response
return data
my_script
errorHandler("https://api.weather.gov/alerts/active")
print "Content type is " + response.headers['content-type'] +".\n" #expect geo+json
# I need the data from the module to do this, but not for each get request
nwsId = data['features'][0]['properties']['id']
错误
Traceback (most recent call last):
File "my_script.py", line 20, in <module>
print errorHandler.response
AttributeError: 'function' object has no attribute 'response'
如果你想 return 多个值,你 return 它们作为单个语句中的元组:
return response, data
然后在调用者中,您将它们分配给具有元组分配的变量:
response, data = errorHandler("https://api.weather.gov/alerts/active")
print "Content type is " + response.headers['content-type'] +".\n"
nwsId = data['features'][0]['properties']['id']
但是,如果发生任何异常,您的函数将无法正常工作。如果出现异常,它不会设置变量 response
或 data
,因此当它尝试 return 时会出现错误。