473,287 Members | 1,581 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,287 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 2498
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...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.