r.headers ['授权']在python2.7类的__call__函数中

时间:2015-09-21 22:23:53

标签: python soap python-requests

我在github上的主kennethreitz / requests上挖掘了一些请求/请求/ auth.py文件。

https://github.com/kennethreitz/requests/blob/master/requests/auth.py

我看到了这段代码,

Private Sub GetContainerButton_Click(sender As System.Object, e As System.EventArgs) Handles GetContainerButton.Click

    Dim di As New IO.DirectoryInfo("C:\folder test")
    Dim diar1 As IO.FileInfo() = di.GetFiles()
    Dim dra As IO.FileInfo
    For Each dra In diar1
        Container.Items.Add(dra)
    Next
End Sub

我无法理解他怎么能想出以前没有定义的r.headerp ['授权']。我错过了什么吗?

非常感谢有人回答这个问题:)

1 个答案:

答案 0 :(得分:0)

我假设r.headers对象只是字典数据结构。

在Python中,您可以使用字典并分配任何属性,无论是否存在,如果它尚未存在,则可以创建它。

如果你启动python shell,你可以看到这个

#Create a new empty dictionary, no attributes
>>> obj = {}
#Assign the string "hello header" to the "headers" attribute
>>> obj['headers'] = "hello header"
# Print it
>>> print(obj['headers'])
  

你好标题

请参阅python词典数据结构的this页面以了解更多信息。

修改

对您链接的源文件进行2秒钟的扫描即可显示

from .utils import parse_dict_header, to_native_string

我认为如果不仔细研究就可以说header属性只是一个字典 - parse_dict_header

编辑2: 要回答有关r.headers来自何处的更具体问题。

__call__方法是调用HTTPBasicAuth对象时的函数,我可以跟踪代码并查看prepare_auth在第{48}行Request } r = auth(self) 对象的方法。

Request

其中self是Request对象实例。 __init__在第225行的self.headers = headers 中设置了自身的headers属性。

Request

models.py中定义的这个r对象是传递给__call__类的HTTPBasicAuth方法的参数int count = -1; Map<Integer , Integer> nodes = new HashMap<>(); nodes.put(src , 1); //count the depth at which the algorithm currently is for(int i = 0 ; i < maxDepth ; i++){ Map<Integer , Integer> next_nodes = new HashMap<>(); nodes.stream.forEach(e -> { if(e.getKey() == dest) count += e.getValue(); for(int j = 0 ; j < arr[e.getKey()].length ; j++) if(arr[e.getKey()][j] > 0) if(next_nodes.containsKey(j)) next_nodes.put(j , next_nodes.get(j) + e.getValue()); else next_nodes.put(j , e.getValue()); }); nodes = next_nodes; }

如果您使用类似Python Visual Studio Calls的内容,您可以运行此代码,打破您感兴趣的行,在这种情况下使用r.headers,并在此处查看backstack并探索范围内的对象。