Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.42!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!46.17.56.130.MISMATCH!rt.uk.eu.org!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.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'string.': 0.04; 'python': 0.08; 'argument,': 0.09; 'called.': 0.09; 'prefixed': 0.09; 'subclass': 0.09; 'typing': 0.15; 'coercion': 0.16; 'open()': 0.16; 'str()': 0.16; 'subclassing': 0.16; 'subject:String': 0.16; 'xxx:': 0.16; 'cc:addr:python-list': 0.16; 'mon,': 0.16; 'wrote:': 0.18; 'string,': 0.18; 'appears': 0.19; 'cheers,': 0.20; 'cc:no real name:2**0': 0.21; 'trying': 0.21; "doesn't": 0.22; 'header :In-Reply-To:1': 0.22; 'feb': 0.22; 'path,': 0.23; 'string': 0.24; 'bruce': 0.24; 'elements': 0.24; 'received:209.85.220': 0.25; 'creating': 0.25; 'right.': 0.25; 'tests': 0.25; "wasn't": 0.25; 'cc:2**0': 0.26; 'function': 0.27; "i'm": 0.28; 'message- id:@mail.gmail.com': 0.29; 'alternatives': 0.29; 'class': 0.29; "skip:' 10": 0.29; 'problem': 0.29; 'cc:addr:python.org': 0.29; 'seem': 0.29; 'pm,': 0.29; 'buffer,': 0.30; 'str': 0.30; 'chris': 0.30; '(as': 0.31; 'opposed': 0.32; "i've": 0.32; 'does': 0.32; 'list': 0.32; 'it.': 0.33; 'instead': 0.33; 'object': 0.33; 'test': 0.34; 'calling': 0.34; 'something': 0.35; 'example,': 0.37; 'but': 0.37; 'received:google.com': 0.37; 'skip:_ 10': 0.38; 'received:209.85': 0.38; 'getting': 0.38; 'e.g.': 0.39; 'except': 0.39; 'are:': 0.39; 'received:209': 0.39; 'your': 0.61; 'you.': 0.62; 'here': 0.64; 'below,': 0.64; 'noise': 0.67; 'genuine': 0.68; 'skip:\xc2 10': 0.74; 'automatic': 0.74; '*have*': 0.84; 'sender:addr:chris': 0.84; 'subject:Automatic': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rebertia.com; s=google; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type :content-transfer-encoding; bh=jmU5jnYXDC+CPIB+irfUq4SVXPxyNdg6xRQbMD2PYX4=; b=BqypVToYVGFSRfQD07+EeB5VaMouxCL4/ZNvXGFec9JGpVwi4ZGZAUkvS+x2PutkbV 25c8fo+REiOuwtACicWj6WsJ6YmMADBosAB+H7mgaKs0TgCCGKiVy9A8RgeYf7cEpWEL aam30IVRZsGNxJlIIdDh5VhZOuqOufIaxM9uM= MIME-Version: 1.0 Sender: chris@rebertia.com In-Reply-To: <090e2893-7a1c-4b11-9e45-974ed33e7b77@i18g2000yqf.googlegroups.com> References: <090e2893-7a1c-4b11-9e45-974ed33e7b77@i18g2000yqf.googlegroups.com> Date: Mon, 13 Feb 2012 14:27:12 -0800 X-Google-Sender-Auth: rbj-AzvqUrLaM6-_J-9_YoCD0Ik Subject: Re: Automatic Type Conversion to String From: Chris Rebert To: Bruce Eckel Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Gm-Message-State: ALoCoQkj5y51Tv9eHjncZaua2Yadhe6meUdGH591ODnyZkbVw2RTOKj9mWY847UGh0kBCFS01/W+ Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 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: 42 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1329172035 news.xs4all.nl 6944 [2001:888:2000:d::a6]:51861 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:20374 On Mon, Feb 13, 2012 at 2:01 PM, Bruce Eckel wrote: > I'm creating a class to encapsulate OS paths, to reduce the visual > noise and typing from the os.path methods. I've got the class and nose > tests below, and everything works except the last test which I've > prefixed with XXX: > > =C2=A0 =C2=A0def XXXtest_should_work(self): > =C2=A0 =C2=A0 =C2=A0 =C2=A0""" > =C2=A0 =C2=A0 =C2=A0 =C2=A0Produces: > =C2=A0 =C2=A0 =C2=A0 =C2=A0open(self.p2, 'w').write('') > =C2=A0 =C2=A0 =C2=A0 =C2=A0TypeError: coercing to Unicode: need string or= buffer, Path > found > =C2=A0 =C2=A0 =C2=A0 =C2=A0""" > =C2=A0 =C2=A0 =C2=A0 =C2=A0open(self.p2, 'w').write('') > =C2=A0 =C2=A0 =C2=A0 =C2=A0assert self.p2 > > Note that I *have* a __str__(self) method to perform automatic > conversion to string, and I've commented out the __unicode__(self) > method because it wasn't getting called. The problem appears to be > that open() does not seem to be calling __str__ on its first argument, > but instead it appears to want a basestring and this doesn't > automatically call __str__. Right. Python is strongly-typed, so it requires a genuine str here (as opposed to just something that's stringifyable) and doesn't do an coercion call to str() for you. Just like how str.join() doesn't automatically call str() on the elements of the list you give it. This is to prevent silliness like trying to open() e.g. a dict. Your alternatives are: - Learn to live with having to write open(str(path_obj)) - Add a .open() method to Path, and use that instead of the open() built-in function - Have Path subclass `str` or `unicode` > I'm trying to write the Path class so I can just hand a Path object > to, for example, open() and have it produce the path string. Impossible, short of subclassing `str` or `unicode`. Cheers, Chris