473,394 Members | 1,960 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

different ways to strip strings

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
6 2515
It is something of a navel (left over feature). "xyz".strip() is (I
think) newer than string.strip()

Feb 27 '06 #2
It is something of a navel (left over feature). "xyz".strip() is (I
think) newer than string.strip()

Feb 27 '06 #3
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: qwweeeit | last post by:
Hi all, I need to limit as much as possible the lenght of a source line, stripping white spaces (except indentation). For example: .. . max_move and AC_RowStack.acceptsCards ( self,...
137
by: Philippe C. Martin | last post by:
I apologize in advance for launching this post but I might get enlightment somehow (PS: I am _very_ agnostic ;-). - 1) I do not consider my intelligence/education above average - 2) I am very...
22
by: ineedyourluvin1 | last post by:
Hello all! I've been looking for a way to strip characters from strings such as a comma. This would be great for using a comma as a delimiter. I show you what I have right now. ...
3
by: Michal A. Valasek | last post by:
Hello, I want to transform text with HTML markup to plain text. Is there some simple way how to do it? I can surely write my own function, which would simply strip everything with < and >....
5
by: dan.j.weber | last post by:
I'm using Python 2.3.5 and when I type the following in the interactive prompt I see that strip() is not working as advertised: >>>s = 'p p:p' >>>s.strip(' :') 'p p:p' Is this just me or...
14
by: BrendanMcPherson | last post by:
Hello, Im try to do some tricks to help make a "Search Many Sites from One Location". http://www.act.org.au/b_nexus.htm What I have decided to do is this: 1. You can choose which site you...
4
by: Ethan Furman | last post by:
Greetings. The strip() method of strings works from both ends towards the middle. Is there a simple, built-in way to remove several characters from a string no matter their location? (besides...
0
by: Calvin Spealman | last post by:
On Jun 16, 2008, at 12:58 PM, Ethan Furman wrote: '.exaple.'
6
by: Christoph Zwerschke | last post by:
In Python programs, you will quite frequently find code like the following for removing a certain prefix from a string: if url.startswith('http://'): url = url Similarly for stripping...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.