By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,799 Members | 1,384 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,799 IT Pros & Developers. It's quick & easy.

different ways to strip strings

P: n/a
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 :)
Feb 27 '06 #1
Share this Question
Share on Google+
6 Replies


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

Feb 27 '06 #2

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

Feb 27 '06 #3

P: n/a
rtilley wrote:
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:
class spam(object): ... def ham(self, eggs):
... print eggs
...

Here is a normal method call: s=spam()
s.ham('Ha!') 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: s.ham <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: f = s.ham
f('Ha!') 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:
spam.ham <unbound method spam.ham>

To call this method, you have to provide the instance as the first argument: f=spam.ham
f(s, 'Ha!')

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
Feb 27 '06 #4

P: n/a
Crutcher wrote:
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
Feb 27 '06 #5

P: n/a
Kent Johnson wrote:
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 :)
Feb 27 '06 #6

P: n/a
On Mon, 27 Feb 2006 13:28:44 -0500, rtilley wrote:
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.
--
Steven.

Feb 28 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.