use*_*970 -1 python django assertions
所以我有一个页面,预计会返回内容或返回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"
Run Code Online (Sandbox Code Playgroud)
返回输出:
200
<type 'int'>
WHAT IS GOING ON!?!?!?!
403
<type 'int'>
Run Code Online (Sandbox Code Playgroud)
显然代码失败assert code is 403但我无法想象为什么.我甚至通过更改线路assert 403 is 403和测试通过来检查自己.我是Python和Django的新手,所以我可能会遗漏一些明显的东西.
In [15]: id(code)
Out[15]: 29536080
In [16]: id(403)
Out[16]: 29535960
Run Code Online (Sandbox Code Playgroud)
使用is,您要问的是代码和403是否是同一个对象.
从ids中可以看出,它们不是.您需要403 == code比较值.
Python缓存小整数的值-5 to 256,这就是为什么你的比较工作200.
当前实现为-5到256之间的所有整数保留一个整数对象数组,当您在该范围内创建一个int时,实际上只返回对现有对象的引用.
| 归档时间: |
|
| 查看次数: |
88 次 |
| 最近记录: |