Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #32956

Re: isinstance(.., file) for Python 3

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 <python-python-list@m.gmane.org>
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 <stefan_ml@behnel.de>
Subject Re: isinstance(.., file) for Python 3
Date Thu, 08 Nov 2012 16:14:37 +0100
References <2pjsm9-p8h.ln1@satorlaser.homedns.org> <XnsA1058E1707A9duncanbooth@127.0.0.1>
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 <XnsA1058E1707A9duncanbooth@127.0.0.1>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.3443.1352387690.27098.python-list@python.org> (permalink)
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

Show key headers only | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

isinstance(.., file) for Python 3 Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2012-11-08 13:05 +0100
  Re: isinstance(.., file) for Python 3 MRAB <python@mrabarnett.plus.com> - 2012-11-08 12:54 +0000
  Re: isinstance(.., file) for Python 3 Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-11-08 12:56 +0000
  Re: isinstance(.., file) for Python 3 Chris Angelico <rosuav@gmail.com> - 2012-11-08 23:57 +1100
  Re: isinstance(.., file) for Python 3 Peter Otten <__peter__@web.de> - 2012-11-08 14:02 +0100
  Re: isinstance(.., file) for Python 3 Duncan Booth <duncan.booth@invalid.invalid> - 2012-11-08 13:58 +0000
    Re: isinstance(.., file) for Python 3 Stefan Behnel <stefan_ml@behnel.de> - 2012-11-08 16:14 +0100

csiph-web