Python:httplib 内令人费解的行为
我在 httplib 的 HTTPConnection.putheader 中添加了一行( import pdb; pdb.set_trace()
),这样我就可以看到里面发生了什么。
Python26\Lib\httplib.py
,第 489 行:
def putheader(self, header, value):
"""Send a request header line to the server.
For example: h.putheader('Accept', 'text/html')
"""
import pdb; pdb.set_trace()
if self.__state != _CS_REQ_STARTED:
raise CannotSendHeader()
str = '%s: %s' % (header, value)
self._output(str)
然后从解释器运行这个
import urllib2
urllib2.urlopen('http://www.ioerror.us/ip/headers')
......并且正如预期的 PDB 启动:
> c:\python26\lib\httplib.py(858)putheader()
-> if self.__state != _CS_REQ_STARTED:
(Pdb)
在 PDB 中,我可以快速评估表达式,所以我有尝试输入 self.__state
:
(Pdb) self.__state
*** AttributeError: HTTPConnection instance has no attribute '__state'
唉,这个实例没有 __state
。 越过该
if self.__state != _CS_REQ_STARTED:
但是,当我输入 step
时,调试器会毫无问题地 行。为什么会发生这种情况?如果 self.__state 不存在,python 将必须引发异常,就像我输入表达式时一样。
Python 版本:win32 上的 2.6.4
I have added one line ( import pdb; pdb.set_trace()
) to httplib's HTTPConnection.putheader, so I can see what's going on inside.
Python26\Lib\httplib.py
, line 489:
def putheader(self, header, value):
"""Send a request header line to the server.
For example: h.putheader('Accept', 'text/html')
"""
import pdb; pdb.set_trace()
if self.__state != _CS_REQ_STARTED:
raise CannotSendHeader()
str = '%s: %s' % (header, value)
self._output(str)
then ran this from the interpreter
import urllib2
urllib2.urlopen('http://www.ioerror.us/ip/headers')
... and as expected PDB kicks in:
> c:\python26\lib\httplib.py(858)putheader()
-> if self.__state != _CS_REQ_STARTED:
(Pdb)
in PDB I have the luxury of evaluating expressions on the fly, so I have tried to enter self.__state
:
(Pdb) self.__state
*** AttributeError: HTTPConnection instance has no attribute '__state'
Alas, there is no __state
of this instance. However when I enter step
, the debugger gets past the
if self.__state != _CS_REQ_STARTED:
line without a problem. Why is this happening? If the self.__state
doesn't exist python would have to raise an exception as it did when I entered the expression.
Python version: 2.6.4 on win32
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
回答我自己的问题:
http://en.wikipedia.org/wiki/Name_mangling#Name_mangling_in_Python
__state
是对象内部的私有名称,它被破坏为_HTTPConnection__state
,所以当我想在 PDB 中访问它时,我必须将其命名为self ._HTTPConnection__状态
。只有对象才能将其引用为__state
。Answering my own question:
http://en.wikipedia.org/wiki/Name_mangling#Name_mangling_in_Python
__state
is a private name inside the object, it gets mangled as_HTTPConnection__state
, so when I want to access it in PDB I have to name it asself._HTTPConnection__state
. Only the object can refer to it as__state
.在 Python 中,您不必显式声明变量。
当您分配给它们时,它们就“诞生”了。
一些代码验证器(例如 pylint)会警告这些情况。
在您的情况下,您可以在
HTTPConnection.__init__()
中有类似self.__state = None
的内容,但这并不是很重要。
In Python, you don't have to declare variables explicitly.
They are "born" when you assign to them.
Some code validators like pylint warn about these situations.
In your case you could have something like
self.__state = None
inHTTPConnection.__init__()
but this is not very important.