Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将多个调用分配给单个变量,否则就性能而言,这并不重要 示例#1: 示例2:_Python_Performance_Python Requests - Fatal编程技术网

Python 将多个调用分配给单个变量,否则就性能而言,这并不重要 示例#1: 示例2:

Python 将多个调用分配给单个变量,否则就性能而言,这并不重要 示例#1: 示例2:,python,performance,python-requests,Python,Performance,Python Requests,如您所见,示例1将请求内容分配给一个变量,然后使用它进行检查 但是示例2没有将其分配给变量,而是继续调用r\u get\u obj.text 我的问题是,第二个代码是否对性能有不良影响/不干净的代码不好,您更喜欢哪一个?您可以在请求库中进行快速浏览和研究,以得出您应该做什么的结论。请注意以下内容: resp=requests.get(“http://google.com") 类型(resp) 产出: <class 'requests.models.Response'> 向下滚动会

如您所见,示例1将请求内容分配给一个变量,然后使用它进行检查

但是示例2没有将其分配给变量,而是继续调用
r\u get\u obj.text


我的问题是,第二个代码是否对性能有不良影响/不干净的代码不好,您更喜欢哪一个?

您可以在
请求
库中进行快速浏览和研究,以得出您应该做什么的结论。请注意以下内容:

resp=requests.get(“http://google.com")
类型(resp)
产出:

<class 'requests.models.Response'>
向下滚动会发现一个名为的属性方法,即您正在调用的方法:

。。。
@财产
def文本(自我):
“”“响应的内容,unicode格式。
如果Response.encoding为None,则将使用
``chardet``。
响应内容的编码仅基于HTTP确定
标题,在RFC2616后面,如果你能利用
非HTTP知识要更好地猜测编码,您应该
在访问此属性之前,请适当设置“r.encoding”。
"""
...
此方法在每次返回结果(和预期的)
text
变量之前进行一些编码;也就是说,它没有缓存。在这种情况下,最好是自己缓存,而不必担心(abuit,次要)性能问题:

resp=requests.get(“http://google.com")
html=resp.text

非常感谢老板,我感谢你的回答没问题,很高兴能帮上忙。
import requests

r_get_obj = requests.get('http://localhost')

if 'junior' in r_get_obj.text:
   print('junior')
elif 'egg' in r_get_obj.text:
   print('egg')
else:
   print('none')
<class 'requests.models.Response'>