Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Matt Wheeler Newsgroups: comp.lang.python Subject: Re: urlopen, six, and py2 Date: Wed, 2 Mar 2016 14:35:20 +0000 Lines: 49 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de JhF30Gp0fT8B7wFYGAq40wB0wUUK/nJIMqPrfw1aImig== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.010 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'context': 0.05; 'finally:': 0.05; 'python3': 0.05; 'resp': 0.07; 'cleaned': 0.09; 'python': 0.10; 'exception': 0.13; 'properly': 0.15; '"with"': 0.16; '*always*': 0.16; '14:05,': 0.16; '2016': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'resp.close()': 0.16; 'resp:': 0.16; 'wrote:': 0.16; 'example.': 0.18; 'skip': 0.18; 'try:': 0.18; 'all,': 0.20; "aren't": 0.22; 'own.': 0.22; 'import': 0.24; 'header:In-Reply-To:1': 0.24; 'question': 0.27; 'message-id:@mail.gmail.com': 0.27; 'feature,': 0.29; 'probably': 0.31; '[1]': 0.32; 'url:python': 0.33; 'except': 0.34; 'handle': 0.34; 'received:google.com': 0.35; 'could': 0.35; 'received:74.125.82': 0.35; 'but': 0.36; 'should': 0.36; 'there': 0.36; 'url:org': 0.36; 'closing': 0.36; 'url:library': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'agree': 0.37; 'expect': 0.37; "won't": 0.38; 'version': 0.38; 'why': 0.39; 'does': 0.39; 'rather': 0.39; 'to:addr:python.org': 0.40; 'your': 0.60; 'leaving': 0.63; 'more': 0.63; 'march': 0.64; 'six': 0.65; 'construction': 0.72; 'from:addr:m': 0.84; 'lying': 0.84; 'subject:six': 0.84; 'absolutely': 0.88; ':).': 0.91; 'anymore,': 0.95 X-Virus-Scanned: Debian amavisd-new at membrane.funkyhat.net X-Gm-Message-State: AD7BkJKfVnHvQKx4qu+nKtM313zuVEAjSSxxl26OlIIe8My8D96LAtX2uiTdvhfRRc6GmmvftHLJTicu1W4tog== X-Received: by 10.194.114.133 with SMTP id jg5mr24493862wjb.99.1456929339761; Wed, 02 Mar 2016 06:35:39 -0800 (PST) In-Reply-To: X-Gmail-Original-Message-ID: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:103872 On 2 March 2016 at 14:05, Fabien wrote: > [snip] > My question is: why does the python3 version need a "with" block while the > python2 version doesn't? Can I skip the "with" entirely, or should I rather > do the following: It's not a case of "need", using the "with" construction is an added feature, not a burden! > from six.moves.urllib.request import urlopen > > try: > with urlopen('http://www.google.com') as resp: > _ = resp.read() > except AttributeError: > # python 2 > resp = urlopen('http://www.google.com') > _ = resp.read() This is poor practise as you aren't closing "resp". This leaves state lying around that you don't need anymore, which is the whole purpose of the context manager that 3 provides. It will *usually* be cleaned up when leaving the current scope, but won't if there is an exception thrown. Using the context manager ensures resp is *always* cleaned up properly even if an exception is thrown. I agree that six should probably handle this, but in the meantime you could use contextlib.closing ([1]) rather than rolling your own. It even uses urlopen as an example. If you absolutely want to roll your own then don't bother with the context manager provided by 3 at all, just use: resp = urlopen('http://www.google.com') try: _ = resp.read() finally: resp.close() But as this is wordier and more fragile than using a context manager to clean up for you, I expect you won't bother :). [1] https://docs.python.org/2/library/contextlib.html#contextlib.closing -- Matt Wheeler http://funkyh.at