str.isspace()

B

bearophileHUGS

Are you using the str.isspace() method? I don't use it, so if most
people don't uses it, then it may be removed from Py 3.0.

I usually need to know if a string contains some non-spaces (not space
class chars). To do it I use something like:

if aline.strip(): ...

If you really need str.isspace() you may use (but so far I have never
had to do this):

if txt and not txt.strip(): ...

Bye,
bearophile
 
S

Sybren Stuvel

(e-mail address removed) enlightened us with:
If you really need str.isspace() you may use (but so far I have never
had to do this):

if txt and not txt.strip(): ...

This looks like an ugly and possibly very inefficient test to me. It
also obfuscates the thing that's actually being tested, while

if txt.isspace():

is pretty obvious.

Sybren
 
F

forman.simon

Are you using the str.isspace() method? I don't use it, so if most
people don't uses it, then it may be removed from Py 3.0.

I usually need to know if a string contains some non-spaces (not space
class chars). To do it I use something like:

if aline.strip(): ...

If you really need str.isspace() you may use (but so far I have never
had to do this):

if txt and not txt.strip(): ...

Bye,
bearophile

A quick check of code in my homedir showed that I've used it about 60
times (that's just in my own code, not work related code.)

In favor of removing it:
* TOOWTDI

In favor of keeping it:

* isspace() returns a Boolean, strip() returns a (newly-allocated)
string that's then just thrown away.

* it's more obvious, "is space?" is exactly what I'm thinking when I
reach for isspace(), it's never occurred to me to use strip() for this
task. Compare:
if offset >= len(line) or line[offset].isspace():
if offset >= len(line) or line[offset].strip():

* it's "symetrical" with the rest of the isfoo() string methods.
IMHO, it would be odd if it wasn't there.

My $0.02


Peace,
~Simon
 
G

George Sakkis

Jean-Paul Calderone said:
Great idea.

By the way, are you using division? I don't use it, so if most people
don't use it, then it may be removed from Py 3.0.

If you really need division, you may use:

x * (y ** -1)

Bye,
Jean-Paul

LOL... well said.
 

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,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top