所以我有一个页面期望返回内容或以Django返回403页面
response = self.client.get(...)
print response.status_code
print type(response.status_code)
assert response.status_code is 200
print "WHAT IS GOING ON!?!?!?!"
response = self.client.get(...)
code = response.status_code
print code
print type(code)
assert code is 403
print "hmm"
返回输出:
200
<type 'int'>
WHAT IS GOING ON!?!?!?!
403
<type 'int'>
显然代码失败了,assert code is 403
但是我无法想象为什么。我什至通过将线路更改为assert 403 is 403
并通过了测试来检查自己。我是Python和Django的新手,所以我可能会缺少明显的东西。
In [15]: id(code)
Out[15]: 29536080
In [16]: id(403)
Out[16]: 29535960
使用is
,您要询问代码和403是否是同一对象。
正如您从ID中看到的那样,它们不是。您需要403 == code
比较值。
Python会从中缓存小整数的值-5 to 256
,这就是为什么您的比较适用于200。
当前的实现为-5到256之间的所有整数保留一个整数对象数组,当您在该范围内创建int时,实际上实际上是返回对现有对象的引用。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句