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


Groups > comp.lang.python > #20374

Re: Automatic Type Conversion to String

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 <chris@rebertia.com>
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 <clp2@rebertia.com>
To Bruce Eckel <lists.eckel@gmail.com>
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 <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.5782.1329172035.27778.python-list@python.org> (permalink)
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

Show key headers only | View raw


On Mon, Feb 13, 2012 at 2:01 PM, Bruce Eckel <lists.eckel@gmail.com> 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:
>
>    def XXXtest_should_work(self):
>        """
>        Produces:
>        open(self.p2, 'w').write('')
>        TypeError: coercing to Unicode: need string or buffer, Path
> found
>        """
>        open(self.p2, 'w').write('')
>        assert 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

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


Thread

Automatic Type Conversion to String Bruce Eckel <lists.eckel@gmail.com> - 2012-02-13 14:01 -0800
  Re: Automatic Type Conversion to String Chris Rebert <clp2@rebertia.com> - 2012-02-13 14:27 -0800
    Re: Automatic Type Conversion to String Bruce Eckel <lists.eckel@gmail.com> - 2012-02-13 15:18 -0800
    Re: Automatic Type Conversion to String Bruce Eckel <lists.eckel@gmail.com> - 2012-02-13 15:18 -0800
      Re: Automatic Type Conversion to String Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2012-02-14 15:26 +0100
        Re: Automatic Type Conversion to String Bruce Eckel <lists.eckel@gmail.com> - 2012-02-15 06:58 -0800
          Re: Automatic Type Conversion to String Ned Deily <nad@acm.org> - 2012-02-15 19:21 +0100

csiph-web