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


Groups > comp.lang.python > #4909

Re: Custom string joining

Path csiph.com!x330-a1.tempe.blueboxinc.net!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed5.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; 'defines': 0.07; 'received:209.85.213.46': 0.07; 'received:mail- yw0-f46.google.com': 0.07; 'advance.': 0.09; 'subject:string': 0.09; 'this:': 0.11; 'am,': 0.14; 'wrote:': 0.14; 'python-list,': 0.16; 'subject:joining': 0.16; '\xc2\xa0i': 0.16; 'cc:no real name:2**0': 0.20; 'cc:2**0': 0.20; 'cheers,': 0.20; 'seems': 0.21; 'header:In-Reply-To:1': 0.22; 'cc:addr:python-list': 0.22; 'received:209.85.213': 0.23; 'objects': 0.24; 'chris': 0.27; 'object': 0.27; 'work.': 0.27; 'message-id:@mail.gmail.com': 0.28; "doesn't": 0.28; 'thanks': 0.29; 'string': 0.29; 'sat,': 0.29; 'skip:" 30': 0.29; 'list': 0.30; 'cc:addr:python.org': 0.31; 'implemented': 0.33; 'sufficient': 0.33; 'skip:" 10': 0.34; 'some': 0.37; 'case': 0.37; 'received:209.85': 0.37; 'received:google.com': 0.38; 'but': 0.38; 'strong': 0.39; 'work?': 0.39; 'received:209': 0.39; 'header:Received:5': 0.40; 'accepts': 0.60; '2011': 0.62; 'conversion': 0.64; 'due': 0.67; 'subject:Custom': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=rebertia.com; s=google; h=domainkey-signature:mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type :content-transfer-encoding; bh=FjjeV5FO8qLj9ur/P3d7f0I2iNUDauyiwDKIbBlXZ4o=; b=a7QyS2hIuebV8/HsPMU8WhRFRmlFI9CyUEziMSdNYd+tijTFPhm4VibsnKpyhf9gij ZIdTkhnVeTlNICYYjR09nF7noxbnLpCq5RIJV8mrIilPlE970FePmhgI85ar8MhZSlyU fAb41P9ZLNfeXW2hp0z+eckbBvw3rJ7LHL1cI=
DomainKey-Signature a=rsa-sha1; c=nofws; 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; b=B25OW4ypkR7GTnu9E358YFhDIdFRz08jA8AZR9/BIJ3cso0dXAgatw1bGfAozOGspJ Br0/8tfAC6FHAhs4y+xpDS/WebpC/aMMew+dnDFbiufWxS/YWIIrY1R3ZjAqMNi21q9g /bw8UuivihZDN4jDLxh77+PVOQO8bUSjtE6uU=
MIME-Version 1.0
Sender chris@rebertia.com
In-Reply-To <254518825.20110507153133@bitdefender.com>
References <254518825.20110507153133@bitdefender.com>
Date Sat, 7 May 2011 07:25:58 -0700
X-Google-Sender-Auth qL5TcdVwZapdCAk59i6CLXoQqZk
Subject Re: Custom string joining
From Chris Rebert <clp2@rebertia.com>
To Claudiu Popa <cpopa@bitdefender.com>
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding quoted-printable
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.1291.1304778361.9059.python-list@python.org> (permalink)
Lines 23
NNTP-Posting-Host 82.94.164.166
X-Trace 1304778361 news.xs4all.nl 34849 [::ffff:82.94.164.166]:35121
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:4909

Show key headers only | View raw


On Sat, May 7, 2011 at 5:31 AM, Claudiu Popa <cpopa@bitdefender.com> wrote:
> Hello Python-list,
>
> I  have  an object which defines some methods. I want to join a list or
> an iterable of those objects like this:
>
> new_string = "|".join(iterable_of_custom_objects)
>
> What   is   the   __magic__  function that needs to be implemented for
> this case to work?  I  though  that  __str__  is sufficient but it doesn't seems to
> work. Thanks in advance.

You need to do the string conversion yourself; .join() doesn't do it
for you, due to strong typing. It only accepts iterables of strings:
new_string = "|".join(str(x) for x in iterable_of_custom_objects)

Cheers,
Chris
--
http://rebertia.com

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


Thread

Re: Custom string joining Chris Rebert <clp2@rebertia.com> - 2011-05-07 07:25 -0700

csiph-web