我在Python 3.1中调试脚本并发现了这个:
(Pdb)p locals(){'count':264,'self':,'depth':1,'offset':0 , '__ return__':无, 'blkno':4,'size':264}
我发现延迟的PEP提到它,而其他一点也没有.
什么是__return__?什么时候加入?它有用吗?
hyn*_*cer 12
它是在评估返回命令后pdb调试器停止时函数调用的返回值.对于具有任何副作用的返回表达式非常重要(无法再现,例如从管道中读取一行).
(Pdb) ... # stop somewhere in the debugger ...
> test.py(3)f()
-> return x + 1
(Pdb) l # list source: I'm just before return
1 def f():
2 x = 7
3 -> return x + 1
(Pdb) '__return__' in locals() # __return__ is still undefined
False
(Pdb) s
--Return--
> test.py(3)f()->8 # This printed 8 is a simple case, but frequently
(Pdb) '__return__' in locals() # the value is an object or line shortened to 80 ch.
True # __return__ has the value after return
(Pdb) __return__
8
Run Code Online (Sandbox Code Playgroud)
如果函数退出而不执行return命令则__return__ == None每次都是.
该__return__关键字仅出现在调试器代码中:
matt@stanley:~/src/Python-3.2$ grep -R __return__ .
./Lib/pdb.py: frame.f_locals['__return__'] = return_value
./Lib/pdb.py: if '__return__' in self.curframe_locals:
./Lib/pdb.py: self.message(repr(self.curframe_locals['__return__']))
./Lib/bdb.py: if '__return__' in frame.f_locals:
./Lib/bdb.py: rv = frame.f_locals['__return__']
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1189 次 |
| 最近记录: |