469,301 Members | 2,071 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,301 developers. It's quick & easy.

Addition to slice syntax (for strings)

Hi,

I'd like to suggest an addition to the slice syntax. How about allowing
strings as arguments to slices on strings, and interpret them as the
index of the substring found within the string being sliced. I'm sure I
didnt explain that too well, so here is an example..
a= "Find the string within the (parenthesis)." #equivalent of a[ a.find('(') : a.find(')', a.find('(') + 1 ) + 1 ] a['(':')'] '(parenthesis)'

#Strings with a negative step would search for the substring from the
end of the string a= 'some/folder/file.txt';
a['.',-1,-1]

'.txt'
I have actually implemented something similar in a C++ string class, so
its not completely crazy - but I would be interested in what more
experienced Python developers think of it.
Thanks,

Will McGugan
Jul 18 '05 #1
2 1370
>>>>> "Will" == Will McGugan <ne**@NOwillmcguganSPAM.com> writes:

Will> I'd like to suggest an addition to the slice syntax. How about
Will> allowing strings as arguments to slices on strings, and interpret
Will> them as the index of the substring found within the string being
Will> sliced. I'm sure I didnt explain that too well, so here is an
Will> example..

How would you interpret
s = "my dog has fleas"
s['x':'z'] # both strings nonexistent
s['z':'f'] # first string nonexistent
s['f':'z'] # second string nonexistent
s['g':'d'] # second appears earlier than first


Returning the empty string in all cases is plausible, but other
possibilities make sense at some level:

s['x':'z'] => ""
s['x':'z'] => "my dog has fleas"
s['z':'f'] => "my dog has f"
s['z':'f'] => "my dog has "
s['f':'z'] => "leas"
s['f':'z'] => "fleas"
s['g':'d'] => ""
s['g':'d'] => "god"

Two other quibbles about your example:

* why not use s.rfind("...") instead of s.find("...") for the second
index?

* why add 1 to the offset of the second index?

In short, there is enough uncertainty in what the "right" thing to do is, I
suspect you'd be better off just subclassing strings when you need this sort
of functionality in your apps.

Skip
Jul 18 '05 #2
Will McGugan wrote:
I have actually implemented something similar in a C++ string class, so
its not completely crazy - but I would be interested in what more
experienced Python developers think of it.


I actually find that it is a good idea for a class, but the devil is in
the details, and I think that it might be too limited in the long run.

Sometimes you want to use rfind() instead of find(). Other times you
want to use a regular expression as the index etc.

It is probably a good idea to leave it out of the language. Especially
as it is pretty easy to roll your own.

A better idea is to add the recipe to the Python Cookbook

http://aspn.activestate.com/ASPN/Python/Cookbook/

regards Max M
Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

15 posts views Thread by Roberto A. F. De Almeida | last post: by
4 posts views Thread by Jens Lippmann | last post: by
108 posts views Thread by Bryan Olson | last post: by
40 posts views Thread by Ron Adam | last post: by
2 posts views Thread by jbperez808 | last post: by
23 posts views Thread by Antoon Pardon | last post: by
7 posts views Thread by Alexandre Guimond | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
1 post views Thread by Geralt96 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.