.join() question

T

Terry Reedy

drs said:
why does

not automatically do
''.join([str(i) for i in lst])

? That is, is there ever a time when one does not want .join to make
everything into a string? This seems like reasonable default behavior, but
maybe I'm missing something?

If the members of lst are already str strings, this would be a waste
of time. If they are unicode strings, this would be the wrong thing
to do. Auto conversions are not Python's style.

Terry J. Reedy
 
D

drs

Terry Reedy said:
drs said:
why does
''.join(lst)

not automatically do
''.join([str(i) for i in lst])

? That is, is there ever a time when one does not want .join to make
everything into a string? This seems like reasonable default behavior, but
maybe I'm missing something?

If the members of lst are already str strings, this would be a waste
of time. If they are unicode strings, this would be the wrong thing
to do. Auto conversions are not Python's style.
l = [u'abcde', 'fghij']
''.join(l)
u'abcdefghij'

this seems to be auto converting the non-unicode to unicode?

-d
 
S

Simon Bayling

I would risk a guess that this comes under the "explicit is better than
implicit" guideline.

''.join([str(i) for i in lst])

Explains to everyone that you know the list has some non-string things,
and you intend them to be str()'ified.

Intention-guessingly yours,
Simon.

why does

not automatically do
''.join([str(i) for i in lst])

? That is, is there ever a time when one does not want .join to make
everything into a string? This seems like reasonable default
behavior, but maybe I'm missing something?

-doug
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top