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


Groups > comp.lang.python > #5022 > unrolled thread

Re: Custom string joining

Started byKarim <karim.liateni@free.fr>
First post2011-05-09 22:14 +0200
Last post2011-05-09 22:14 +0200
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

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

#5022 — Re: Custom string joining

FromKarim <karim.liateni@free.fr>
Date2011-05-09 22:14 +0200
SubjectRe: Custom string joining
Message-ID<mailman.1353.1304972530.9059.python-list@python.org>
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

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web