Path: csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'subject:Python': 0.05; 'parameter': 0.07; 'subject:file': 0.07; 'try:': 0.07; 'python': 0.09; 'bytes)': 0.09; 'file-like': 0.09; 'nameerror:': 0.09; 'parameter.': 0.09; 'tuple': 0.09; 'file,': 0.15; '(str,': 0.16; 'basestring):': 0.16; 'different;': 0.16; 'eckhardt': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'happily': 0.16; 'semantically': 0.16; 'string': 0.17; 'wrote:': 0.17; 'bytes': 0.17; 'thu,': 0.17; 'unicode': 0.17; 'subject:) ': 0.20; 'file.': 0.20; 'received:209.85.214.174': 0.21; 'object.': 0.22; 'allows': 0.25; 'pass': 0.25; 'header:In-Reply-To:1': 0.25; 'common': 0.26; 'message-id:@mail.gmail.com': 0.27; 'types.': 0.29; 'code': 0.31; 'file': 0.32; 'safely': 0.33; 'to:addr:python- list': 0.33; 'likely': 0.33; 'received:google.com': 0.34; 'nov': 0.35; 'pm,': 0.35; 'received:209.85': 0.35; 'except': 0.36; 'but': 0.36; 'received:209': 0.37; 'subject:: ': 0.38; 'fact': 0.38; 'object': 0.38; 'to:addr:python.org': 0.39; 'received:209.85.214': 0.39; 'skip:" 10': 0.40; 'header:Received:5': 0.40; "you'll": 0.62; 'more': 0.63 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=LzEJ1XDJfmVus/M8ZP+FP3gUBsPyaJ9TmEqH2AGIPHM=; b=KrE7p+IAxLNj9jv//5jZkAnpH6W3C1heGPSNX5Z5uMul0k6qrBMrtQciw/jILyj4Yu 6TA6es9712veeJkvq9zAPRbEd7Huk7Mp2ZtamQtGEpNxjuh8rDlOsrtXggCb9LeXQDqj FedMmdF6nmZ4y69dYiIaAYFiupX4gRCRp51R13/Jlu2v97MLuDgPtEo6k24/nVeGKCIb lTcOWqlzbjJL4Wv+1ZpVJzHlsEnPABfYf6QyfnUOy0592EjXxWhrMY+P+a135IBVJIe4 a0lfQZloc5LuPAkKGqMPSUGhwGDEMcw9APNMm8tpwvfJ2M+j92y4U09bB3+i8IJhAKpC 1NhA== MIME-Version: 1.0 In-Reply-To: <2pjsm9-p8h.ln1@satorlaser.homedns.org> References: <2pjsm9-p8h.ln1@satorlaser.homedns.org> Date: Thu, 8 Nov 2012 23:57:00 +1100 Subject: Re: isinstance(.., file) for Python 3 From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 28 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1352379430 news.xs4all.nl 6841 [2001:888:2000:d::a6]:39958 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:32946 On Thu, Nov 8, 2012 at 11:05 PM, Ulrich Eckhardt wrote: > 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... Can you use the inverted check "isinstance(p, str)"? It's more likely that you'll want to pass a file-like object than a string-like object. This would work on Python 2 as well, though it's semantically different; to safely check for both Unicode and bytes strings on both Py2 and Py3, this may work: # Once-off: try: basestring except NameError: basestring = (str, bytes) # Is p a string? if isinstance(p, basestring): pass It abuses the fact that isinstance will happily accept the 'basestring' common supertype of both strings in Python 2, but will equally happily accept a tuple of types. ChrisA