Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'subject:Python': 0.05; 'subject:file': 0.07; 'try:': 0.07; 'read()': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'def': 0.10; 'all?': 0.16; 'booth,': 0.16; 'eckhardt': 0.16; 'from:addr:behnel.de': 0.16; 'from:addr:stefan_ml': 0.16; 'from:name:stefan behnel': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'wrote:': 0.17; 'stefan': 0.17; 'subject:) ': 0.20; 'bit': 0.21; "i'd": 0.22; 'header:In-Reply- To:1': 0.25; 'header:User-Agent:1': 0.26; 'checking': 0.27; 'older': 0.27; 'possible,': 0.27; 'header:X-Complaints-To:1': 0.28; 'though.': 0.29; "i'm": 0.29; 'received:84': 0.32; 'to:addr :python-list': 0.33; 'or,': 0.34; 'received:org': 0.36; 'except': 0.36; 'why': 0.37; 'previous': 0.37; 'rather': 0.37; 'data': 0.37; 'subject:: ': 0.38; 'some': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'more': 0.63; 'received:arcor-ip.net': 0.84; 'received:pools.arcor-ip.net': 0.84; 'shedding': 0.84; 'skin.': 0.84; 'snake': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Stefan Behnel Subject: Re: isinstance(.., file) for Python 3 Date: Thu, 08 Nov 2012 16:14:37 +0100 References: <2pjsm9-p8h.ln1@satorlaser.homedns.org> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: dslb-084-056-012-087.pools.arcor-ip.net User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:16.0) Gecko/20121028 Thunderbird/16.0.2 In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 31 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1352387690 news.xs4all.nl 6955 [2001:888:2000:d::a6]:50938 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:32956 Duncan Booth, 08.11.2012 14:58: > Ulrich Eckhardt wrote: >> If possible, I'm looking for a solution that works for Pythons 2 and 3, >> since I'm not fully through the conversion yet and have clients that >> might use the older snake for some time before shedding their skin. >> >> Suggestions? > > Why bother checking types at all? > > def foo(file_or_string): > try: > data = file_or_string.read() > except AttributeError: > data = file_or_string > ... use data ... Or, a tiny bit more safely: try: read = file_or_string.read except AttributeError: data = file_or_string else: data = read() I'd rather go with one of the previous solutions, though. Stefan