How do i reduce this to a single function - the code is largely similar, just a direction of search toggle.

Ned Batchelder ned at
Sat Nov 8 13:56:46 CET 2014

On 11/7/14 9:52 AM, Veek M wrote:
> Veek M wrote:
>>                  new_col = self.b[row].index('def')
>>                  self.w.cursor = row, new_col
>>                  new_col = self.b[row].rindex('def')
>>                  self.w.cursor = row, new_col
> There's also the different methods index vs rindex. Does this sort of thing
> justify two functions and associated infrastructure (it's for vim so 2 x 3
> mode lines in my .vimrc)

Stepping back a bit from the lines of code, why do you need to use 
.index in one case and .rindex in the other?  You are trying to find the 
"def" line in the editor buffer, either above the current point, or 
below it.  But the "def" will always be the first non-whitespace text on 
the line.   In fact, when searching upward, you could find this line:

     def get_default(self):

and you want to end up on the "def" token, not the "def" in 
"get_default".  .rindex isn't what you want in any case.  Use .index in 
both cases.

Ned Batchelder,

More information about the Python-list mailing list