Path: csiph.com!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'broken': 0.03; 'subject:Python': 0.05; 'exist,': 0.07; 'parameter': 0.07; 'subject:file': 0.07; 'python': 0.09; 'open()': 0.09; 'parameter.': 0.09; 'file,': 0.15; 'eckhardt': 0.16; 'fits': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'string': 0.17; 'wrote:': 0.17; 'fix': 0.17; 'code,': 0.18; 'code.': 0.20; 'subject:) ': 0.20; 'file.': 0.20; 'together.': 0.21; "i'd": 0.22; 'allows': 0.25; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'checking': 0.27; 'older': 0.27; 'possible,': 0.27; "doesn't": 0.28; 'received:192.168.1.3': 0.29; 'skip:_ 10': 0.29; 'class': 0.29; "i'm": 0.29; 'related': 0.30; 'code': 0.31; 'file': 0.32; 'to:addr:python-list': 0.33; "can't": 0.34; 'built-in': 0.35; 'but': 0.36; 'problems': 0.36; 'two': 0.37; 'subject:: ': 0.38; 'object': 0.38; 'some': 0.38; 'instead': 0.39; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'skip:" 10': 0.40; 'received:192.168': 0.40; 'solve': 0.62; 'header:Reply- To:1': 0.68; 'touch': 0.69; 'reply-to:no real name:2**0': 0.72; 'reply-to:addr:python.org': 0.84; 'shedding': 0.84; 'skin.': 0.84; 'snake': 0.84; 'ugly,': 0.84 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.0 cv=TdYURGsh c=1 sm=1 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=1EKJ2blYo8kA:10 a=m6aeIBemjaYA:10 a=ihvODaAuJD4A:10 a=OUOv7kDek9cA:10 a=8nJEP1OIZ-IA:10 a=EBOSESyhAAAA:8 a=8AHkEIZyAAAA:8 a=6JP9E4TWjUcA:10 a=rXWL0u0-ptU-T_lHOAsA:9 a=wPNLvfGTeEIA:10 a=0nF1XD0wxitMEM03M9B4ZQ==:117 X-AUTH: mrabarnett:2500 Date: Thu, 08 Nov 2012 12:54:15 +0000 From: MRAB User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:16.0) Gecko/20121026 Thunderbird/16.0.2 MIME-Version: 1.0 To: python-list@python.org Subject: Re: isinstance(.., file) for Python 3 References: <2pjsm9-p8h.ln1@satorlaser.homedns.org> In-Reply-To: <2pjsm9-p8h.ln1@satorlaser.homedns.org> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: python-list@python.org 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: 25 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1352379260 news.xs4all.nl 6987 [2001:888:2000:d::a6]:37459 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:32943 On 2012-11-08 12:05, Ulrich Eckhardt wrote: > Hi! > > I have two problems that are related and that I'd like to solve together. > > Firstly, I have code that allows either a file or a string representing > its content as parameter. If the parameter is a file, the content is > read from the file. In Python 2, I used "isinstance(p, file)" to > determine whether the parameter p is a file. In Python 3, the > returnvalue of open() is of type _io.TextIOWrapper, while the built-in > class file doesn't exist, so I can't use that code. > > Secondly, checking for the type is kind-of ugly, because it means that I > can't use an object that fits but that doesn't have the right type. In > other words, it breaks duck-typing. This is already broken in the Python > 2 code, but since I have to touch the code anyway, I might as well fix > it on the way. > > 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? > Instead of checking whether it's a file, check whether it's a string!