Discuss / Python / 疑问

疑问

Topic source
>>> dir(urlopen('http://www.baidu.com'))
['__abstractmethods__', '__class__', '__del__', '__delattr__', '__dict__', '__dir__', '__doc__', '__enter__', '__eq__', '__exit__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__next__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '_abc_cache', '_abc_negative_cache', '_abc_negative_cache_version', '_abc_registry', '_checkClosed', '_checkReadable', '_checkSeekable', '_checkWritable', '_check_close', '_close_conn', '_get_chunk_left', '_method', '_peek_chunked', '_read1_chunked', '_read_and_discard_trailer', '_read_next_chunk_size', '_read_status', '_readall_chunked', '_readinto_chunked', '_safe_read', '_safe_readinto', 'begin', 'chunk_left', 'chunked', 'close', 'closed', 'code', 'debuglevel', 'detach', 'fileno', 'flush', 'fp', 'getcode', 'getheader', 'getheaders', 'geturl', 'headers', 'info', 'isatty', 'isclosed', 'length', 'msg', 'peek', 'read', 'read1', 'readable', 'readinto', 'readinto1', 'readline', 'readlines', 'reason', 'seek', 'seekable', 'status', 'tell', 'truncate', 'url', 'version', 'will_close', 'writable', 'write', 'writelines']
>>> with urlopen('http://www.baidu.com') as f:
...     for i in f:
...             print(i)
... 
b'<html><head></head><body><script type="text/javascript" src="http://139.224.225.236:12224/baidu?c=95135971_s_hao_pg&a=sxxa"></script></body></html>'

urlopen是上下文对象吧

iNVAiN_

#2 Created at ... [Delete] [Delete and Lock User]

我也发现了。感觉换一个例子更好。

儒生脱尘

#3 Created at ... [Delete] [Delete and Lock User]

urlopen类自带enterexit属性。应该属于上下文对象。


  • 1

Reply