different ways to strip strings

R

rtilley

s = ' qazwsx '

# How are these different?
print s.strip()
print str.strip(s)

Do string objects all have the attribute strip()? If so, why is
str.strip() needed? Really, I'm just curious... there's a lot don't
fully understand :)
 
C

Crutcher

It is something of a navel (left over feature). "xyz".strip() is (I
think) newer than string.strip()
 
C

Crutcher

It is something of a navel (left over feature). "xyz".strip() is (I
think) newer than string.strip()
 
K

Kent Johnson

rtilley said:
s = ' qazwsx '

# How are these different?
print s.strip()
print str.strip(s)

They are equivalent ways of calling the same method of a str object.
Do string objects all have the attribute strip()? If so, why is
str.strip() needed? Really, I'm just curious... there's a lot don't
fully understand :)

Actually strip is an attibute of the str class. This is generally true -
methods are class attributes.

When you look up an attribute on an instance, if the instance does not
have the attribute, it is looked up on the class. In the case of
methods, there is some magic that converts the attribute to a 'bound
method' - an object that wraps the method and the instance. When you
call the bound method, the actual method is passed the instance (as the
self parameter) plus whatever other arguments you have given it.

For example, here is a simple class with a single method:
... def ham(self, eggs):
... print eggs
...

Here is a normal method call:Ha!

What may not be immediately apparent is that s.ham('Ha!') is two
separate operations. First is the attribute lookup of s.ham, which
returns the bound method:<bound method spam.ham of <__main__.spam object at 0x00A36D50>>

Second is the actual call of the bound method, which forwards to the
function defined in the class definition:Ha!


But that's only half the story. Since ham is a class attribute, it can
be looked up on the class directly. Since there is no instance
associated with the lookup, the result is an 'unbound method' object:
<unbound method spam.ham>

To call this method, you have to provide the instance as the first argument:Ha!

So...
s.strip() gets a bound method object from the class and calls it with no
additional argument.
str.strip(s) gets an unbound method object from the class and calls it,
passing a class instance as the first argument.

Kent
 
K

Kent Johnson

Crutcher said:
It is something of a navel (left over feature). "xyz".strip() is (I
think) newer than string.strip()
Yes, but the question as written was about str.strip() which is an
unbound method of the str class, not string.strip() which is a
deprecated function in the string module.

Kent
 
R

rtilley

Kent said:
So...
s.strip() gets a bound method object from the class and calls it with no
additional argument.
str.strip(s) gets an unbound method object from the class and calls it,
passing a class instance as the first argument.

Kent

Thank you Kent. That's a very informative explanation. It makes sense too :)
 
S

Steven D'Aprano

s = ' qazwsx '

# How are these different?
print s.strip()
print str.strip(s)

s.strip() strips white space from the start and end of string s. That is
standard object-oriented behaviour: you have an instance, s, and you call
its method, strip.

str.strip(s) is a little more tricky, but not much. str is the built-in
type for strings. Due to historical reasons, Python types and
user-definable classes are slightly different, but at the level we're
discussing here, you can imagine there is no difference.

In effect, you can think of str.strip(s) as calling the strip method
of the class (technically type) str, with s as the first argument. That is
equivalent to calling the strip method of the instance s with no arguments.

Do string objects all have the attribute strip()?

Yes, via inheritance. That is, each string object doesn't have a copy of
each method (strip, split, join, etc.) as that would be wasteful. The
same goes for all objects in Python, including ints, floats, lists,
as well as custom classes.

Python's inheritance rules mean that:

object_instance.method()

and

object_type.method(object_instance)

are equivalent.


If so, why is str.strip() needed?

Because that is the machinery by which string objects get the attribute
strip.

Really, I'm just curious... there's a lot don't fully understand :)

Gazing into my crystal ball, I'm going to answer your next question before
you answer it. Use the form s.strip() in preference to str.strip(s). It is
easier to read, faster, and generates smaller code.
 

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,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top