2014-06-12 47 views
1

我正在使用urllib2發送GET和POST請求到帶有參數的服務器,但對於一些請求,我不需要知道服務器的響應,除了它不是404的事實,並且urlopen引發異常404。但是,如果我只是使用該行:python函數是否關心你是否使用它們的輸出?

urllib2.urlopen(address, params) 

服務器看不到請求。就好像它永遠不會被髮送一樣。但是,如果我那麼做

response = urllib2.urlopen(address, params) 

命令通過。我看了看源的urllib2,發現功能的urlopen,它看起來像這樣:

def urlopen(url, data=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT): 
    ... 
    return _opener.open(url, data, timeout) 

好像是_opener.open功能沒有運行的時候我不存儲的urlopen的輸出。如果我不要求函數的輸出,python是否不執行return語句?

+0

作爲返回值的分配情況發生*之後*被執行的功能,這是不可能的功能,以瞭解其是否返回價值被賦予某種東西。正如Martijn所說,你的錯誤在別處。 – poke

+0

@MartijnPieters這不完全正確,有許多使用yield的函數,除非你用它做什麼,否則不會做任何有意義的事情。 – Soviero

+0

@Soviero:那些是發電機功能。他們**做**返回東西,一個發電機對象。但是urlopen()不是生成器函數。 –

回答

3

不,函數的行爲不會根據是否存儲對返回值的引用進行更改,或者完全忽略它。在返回值被存儲或忽略之前,該函數被執行。如果函數調用導致引發異常,則根本不會有返回值。

您對發生的事情的分析不正確,問題在於別處。

你可以很容易地這個自己雖然測試:

>>> def foo(): 
...  print "foo() called" 
... 
>>> def bar(): 
...  return foo() 
... 
>>> bar() 
foo() called 
>>> result = bar() 
foo() called 

,或者使用urllib2.urlopen()本身,使用http://httpbin.org/status/404觸發404未找到響應:

>>> import urllib2 
>>> response = urllib2.urlopen('http://httpbin.org/status/404') 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "/Users/mj/Development/Library/buildout.python/parts/opt/lib/python2.7/urllib2.py", line 127, in urlopen 
    return _opener.open(url, data, timeout) 
    File "/Users/mj/Development/Library/buildout.python/parts/opt/lib/python2.7/urllib2.py", line 410, in open 
    response = meth(req, response) 
    File "/Users/mj/Development/Library/buildout.python/parts/opt/lib/python2.7/urllib2.py", line 523, in http_response 
    'http', request, response, code, msg, hdrs) 
    File "/Users/mj/Development/Library/buildout.python/parts/opt/lib/python2.7/urllib2.py", line 448, in error 
    return self._call_chain(*args) 
    File "/Users/mj/Development/Library/buildout.python/parts/opt/lib/python2.7/urllib2.py", line 382, in _call_chain 
    result = func(*args) 
    File "/Users/mj/Development/Library/buildout.python/parts/opt/lib/python2.7/urllib2.py", line 531, in http_error_default 
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp) 
urllib2.HTTPError: HTTP Error 404: NOT FOUND 
>>> urllib2.urlopen('http://httpbin.org/status/404') 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "/Users/mj/Development/Library/buildout.python/parts/opt/lib/python2.7/urllib2.py", line 127, in urlopen 
    return _opener.open(url, data, timeout) 
    File "/Users/mj/Development/Library/buildout.python/parts/opt/lib/python2.7/urllib2.py", line 410, in open 
    response = meth(req, response) 
    File "/Users/mj/Development/Library/buildout.python/parts/opt/lib/python2.7/urllib2.py", line 523, in http_response 
    'http', request, response, code, msg, hdrs) 
    File "/Users/mj/Development/Library/buildout.python/parts/opt/lib/python2.7/urllib2.py", line 448, in error 
    return self._call_chain(*args) 
    File "/Users/mj/Development/Library/buildout.python/parts/opt/lib/python2.7/urllib2.py", line 382, in _call_chain 
    result = func(*args) 
    File "/Users/mj/Development/Library/buildout.python/parts/opt/lib/python2.7/urllib2.py", line 531, in http_error_default 
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp) 
urllib2.HTTPError: HTTP Error 404: NOT FOUND 

兩項行動引發異常。

+3

這個測試不是很有用,因爲可能有(理論上)特殊的檢測方法,告訴你返回值是否存儲。 – poke

+0

@poke:你可以使用'__del__'來檢測引用計數是否降到0。但是,作爲語言的Python在調用者要放棄返回對象時不會通過執行「return」表達式來「優化」。 –

+0

我明顯知道這是不可能的。我的觀點是 - 正如我所說 - 理論上可能有某些東西;並且不知道這一點,你不能寫一個測試它是否有效的例子。 – poke

0

Python會一直執行return語句,無論你是否使用的返回值

相關問題