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


Groups > comp.lang.python > #5022

Re: Custom string joining

Path csiph.com!x330-a1.tempe.blueboxinc.net!aioe.org!feeder.news-service.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <karim.liateni@free.fr>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.003
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'defines': 0.07; 'python': 0.07; 'advance.': 0.09; 'subject:string': 0.09; 'this:': 0.11; 'am,': 0.14; 'wrote:': 0.14; 'from:addr:free.fr': 0.16; 'python- list,': 0.16; 'rebert': 0.16; 'received:212.27': 0.16; 'received:212.27.42': 0.16; 'received:212.27.42.10': 0.16; 'received:free.fr': 0.16; 'received:smtpfb2-g21.free.fr': 0.16; 'subject:joining': 0.16; 'cc:no real name:2**0': 0.20; 'cheers,': 0.20; 'seems': 0.21; 'header:In-Reply-To:1': 0.22; 'cc:addr :python-list': 0.22; 'objects': 0.24; 'chris': 0.27; 'object': 0.27; 'function': 0.27; 'work.': 0.27; "doesn't": 0.28; 'thanks': 0.29; 'string': 0.29; 'sat,': 0.29; 'skip:" 30': 0.29; 'list': 0.30; 'implement': 0.30; 'cc:addr:python.org': 0.31; 'implemented': 0.33; 'sufficient': 0.33; 'received:192': 0.34; 'skip:" 10': 0.34; 'received:192.168.0': 0.35; 'header:User- Agent:1': 0.35; 'received:192.168': 0.37; 'some': 0.37; 'case': 0.37; 'but': 0.38; 'cc:2**1': 0.38; 'though': 0.38; 'strong': 0.39; 'work?': 0.39; 'accepts': 0.60; '2011': 0.62; 'conversion': 0.64; 'special': 0.66; 'due': 0.67; 'subject:Custom': 0.84
Date Mon, 09 May 2011 22:14:38 +0200
From Karim <karim.liateni@free.fr>
User-Agent Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.17) Gecko/20110424 Thunderbird/3.1.10
MIME-Version 1.0
To Chris Rebert <clp2@rebertia.com>
Subject Re: Custom string joining
References <254518825.20110507153133@bitdefender.com> <BANLkTim-94Zfq1o0P5DnAKfywR=cTm2z9Q@mail.gmail.com>
In-Reply-To <BANLkTim-94Zfq1o0P5DnAKfywR=cTm2z9Q@mail.gmail.com>
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 7bit
Cc Claudiu Popa <cpopa@bitdefender.com>, 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.1353.1304972530.9059.python-list@python.org> (permalink)
Lines 26
NNTP-Posting-Host 82.94.164.166
X-Trace 1304972530 news.xs4all.nl 81485 [::ffff:82.94.164.166]:53165
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:5022

Show key headers only | View raw


On 05/07/11 16:25, Chris Rebert wrote:
> 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)

You just have to implement __str__() python special method for your 
"custom_objects".

Regards
Karim
> Cheers,
> Chris
> --
> http://rebertia.com

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


Thread

Re: Custom string joining Karim <karim.liateni@free.fr> - 2011-05-09 22:14 +0200

csiph-web