Addition to slice syntax (for strings)

W

Will McGugan

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
 
S

Skip Montanaro

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
 
M

Max M

Will said:
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
 

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top