Python 处理来自http get调用的空响应
我在self.method中有一个http get调用,在某些情况下,它在响应链接中只返回[]。我想用这个输出在另一个方法中处理异常,比如Python 处理来自http get调用的空响应,python,rest,Python,Rest,我在self.method中有一个http get调用,在某些情况下,它在响应链接中只返回[]。我想用这个输出在另一个方法中处理异常,比如 if type(self.method()) is 'NoneType': print "The object is not present" else: self.method2() 我犯了错误 像 如何在if语句中捕获响应输出[]?假设在获取失败时self.method返回[]: 此代码段使用了这样一个事实,即隐式bool被强制转
if type(self.method()) is 'NoneType':
print "The object is not present"
else:
self.method2()
我犯了错误
像
如何在if语句中捕获响应输出[]?假设在获取失败时self.method返回[]:
此代码段使用了这样一个事实,即隐式bool被强制转换为响应,如果列表为空,则该响应的计算结果为False。它基本上是一个可读性更强的版本:if response=[]您是来自JavaScript吗?进行此检查的python方法是self.method是否为None。或者如果self.method方法不能返回错误的值,如0、False或[]。非常感谢它的工作
AttributeError: 'Response' object has no attribute 'URL'
response = self.method()
if not response:
print 'response missing'
else:
do_things(reponse)