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


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

Re: Custom string joining

Started byClaudiu Popa <cpopa@bitdefender.com>
First post2011-05-09 23:25 +0300
Last post2011-05-09 14:38 -0700
Articles 2 — 2 participants

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 Claudiu Popa <cpopa@bitdefender.com> - 2011-05-09 23:25 +0300
    Re: Custom string joining Martineau <ggrp2.20.martineau@dfgh.net> - 2011-05-09 14:38 -0700

#5024 — Re: Custom string joining

FromClaudiu Popa <cpopa@bitdefender.com>
Date2011-05-09 23:25 +0300
SubjectRe: Custom string joining
Message-ID<mailman.1355.1304972738.9059.python-list@python.org>
Hello Karim,


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

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

I  already told in the first post that I've implemented __str__ function, but it doesn't
seems to be automatically called.
For instance, the following example won't work:

>>> class a:
        def __init__(self, i):
                self.i = i
        def __str__(self):
                return "magic_function_{}".format(self.i)
>>> t = a(0)
>>> str(t)
'magic_function_0'
>>> "".join([t])
Traceback (most recent call last):
  File "<pyshell#13>", line 1, in <module>
    "".join([t])
TypeError: sequence item 0: expected str instance, a found

-- 
Best regards,
 Claudiu

[toc] | [next] | [standalone]


#5035

FromMartineau <ggrp2.20.martineau@dfgh.net>
Date2011-05-09 14:38 -0700
Message-ID<b0f6e092-5242-475a-926b-d8a2439004f8@h12g2000pro.googlegroups.com>
In reply to#5024
On May 9, 1:25 pm, Claudiu Popa <cp...@bitdefender.com> wrote:
> Hello Karim,
>
> > You just have to implement __str__() python special method for your
> > "custom_objects".
> > Regards
> > Karim
> >> Cheers,
> >> Chris
> >> --
> >>http://rebertia.com
>
> I  already told in the first post that I've implemented __str__ function, but it doesn't
> seems to be automatically called.
> For instance, the following example won't work:
>
> >>> class a:
>
>         def __init__(self, i):
>                 self.i = i
>         def __str__(self):
>                 return "magic_function_{}".format(self.i)>>> t = a(0)
> >>> str(t)
> 'magic_function_0'
> >>> "".join([t])
>
> Traceback (most recent call last):
>   File "<pyshell#13>", line 1, in <module>
>     "".join([t])
> TypeError: sequence item 0: expected str instance, a found
>
> --
> Best regards,
>  Claudiu

The built-in str join() method just doesn't do the conversion to
string automatically -- instead it assume it has been given a sequence
of string. You can achieve the effect you want by modifying the
optimized version of my earlier post that @Ian Kelly submitted.

def join(seq, sep):
    it = iter(seq)
    sep = str(sep)
    try:
        first = it.next()
    except StopIteration:
        return []
    def add_sep(r, v):
        r += [sep, str(v)]
        return r
    return ''.join(reduce(add_sep, it, [str(first)]))

class A:
    def __init__(self, i):
        self.i = i
    def __str__(self):
        return "magic_function_{}".format(self.i)

lst = [A(0),A(1),A('b'),A(3)]

print repr( join(lst, '|') )

Output:

'magic_function_0|magic_function_1|magic_function_b|magic_function_3'

[toc] | [prev] | [standalone]


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


csiph-web