Python: puzzling behaviour inside httplib
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 s开发者_运维问答elf.__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
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 as self._HTTPConnection__state
. Only the object can refer to it as __state
.
If the self.__state doesn't exist python would have to raise an exception as it did when I entered the expression.
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
in HTTPConnection.__init__()
but this is not very important.
精彩评论