Why ' '.some_string is often used ?

  • Thread starter Stéphane Ninin
  • Start date
S

Stéphane Ninin

Hi all,

This is not the first time I see this way of coding in Python and
I wonder why this is coded this way:

Howto on PyXML
(http://pyxml.sourceforge.net/topics/howto/node14.html)
shows it on this function, but I saw that in many other pieces of code:

def normalize_whitespace(text):
"Remove redundant whitespace from a string"
return ' '.join(text.split())

Is there a reason to do instead of just returning join(text.split()) ?
what does this mean ?

Thanks in advance for your explanations.

Regards,
 
J

Jacek Generowicz

Stéphane Ninin said:
Hi all,

This is not the first time I see this way of coding in Python and
I wonder why this is coded this way:

Howto on PyXML
(http://pyxml.sourceforge.net/topics/howto/node14.html)
shows it on this function, but I saw that in many other pieces of code:

def normalize_whitespace(text):
"Remove redundant whitespace from a string"
return ' '.join(text.split())

Is there a reason to do instead of just returning join(text.split()) ?

One reason for preferring the former over the latter might be that the
former works, while the latter doesn't:

Traceback (most recent call last):
File "<stdin>", line 1, in ?
NameError: name 'join' is not defined

You can make it work of course ...
'this is a late parrot'

But you shouln't really be polluting your globals with stuff from
modules, so you'd probably prefer to

import string

rather than

from string import join

(or, heaver forbid

from string import *

!)

In which case you would end up writing

string.join(text.split())

which is a whole whopping 3 characters longer than

' '.join(text.split())

:)

If you are too zealous in your interpretation of the second line of
the Zen of Python[*], then you might also prefer the latter as it is
more explicit than the former.




[*] try:
 
L

Lawrence Oluyede

Stéphane Ninin said:
def normalize_whitespace(text):
"Remove redundant whitespace from a string"
return ' '.join(text.split())

Is there a reason to do instead of just returning join(text.split()) ?

join is a function of string module and it belongs also to
string object. So you can't do join() but you have to do

str = " "
str.join(text.split())

or

import string
string.join(text.split(), " ")

Take a look: http://python.org/doc/current/lib/module-string.html
 
D

Dan Bishop

Lawrence Oluyede said:
join is a function of string module and it belongs also to
string object. So you can't do join() but you have to do

str = " "

Don't do that. It clobbers the builtin name "str".
str.join(text.split())

That doesn't change the string that "str" points to. You meant

s = " "
s = s.join(text.split())

Another alternative syntax without using the string module is

s = str.join(" ", text.split())
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top