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


Groups > comp.lang.python > #4909

Re: Custom string joining

References <254518825.20110507153133@bitdefender.com>
Date 2011-05-07 07:25 -0700
Subject Re: Custom string joining
From Chris Rebert <clp2@rebertia.com>
Newsgroups comp.lang.python
Message-ID <mailman.1291.1304778361.9059.python-list@python.org> (permalink)

Show all headers | 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