Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #80398

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

From Ned Batchelder <ned@nedbatchelder.com>
Subject Re: How do i reduce this to a single function - the code is largely similar, just a direction of search toggle.
Date 2014-11-08 07:56 -0500
References <m3i9lp$8fp$1@dont-email.me> <m3insb$s06$1@dont-email.me> <m3iprd$8aq$1@dont-email.me>
Newsgroups comp.lang.python
Message-ID <mailman.15581.1415451417.18130.python-list@python.org> (permalink)

Show all headers | View raw


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, http://nedbatchelder.com

Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

How do i reduce this to a single function - the code is largely similar, just a direction of search toggle. Veek M <vek.m1234@gmail.com> - 2014-11-07 16:46 +0630
  Re: How do i reduce this to a single function - the code is largely similar, just a direction of search toggle. Joel Goldstick <joel.goldstick@gmail.com> - 2014-11-07 16:18 -0500
  Re: How do i reduce this to a single function - the code is largely similar, just a direction of search toggle. Denis McMahon <denismfmcmahon@gmail.com> - 2014-11-07 22:22 +0000
  Re: How do i reduce this to a single function - the code is largely similar, just a direction of search toggle. Chris Angelico <rosuav@gmail.com> - 2014-11-08 09:31 +1100
  Re: How do i reduce this to a single function - the code is largely similar, just a direction of search toggle. Veek M <vek.m1234@gmail.com> - 2014-11-07 21:22 +0630
    Re: How do i reduce this to a single function - the code is largely similar, just a direction of search toggle. Ned Batchelder <ned@nedbatchelder.com> - 2014-11-08 07:56 -0500
      Re: How do i reduce this to a single function - the code is largely similar, just a direction of search toggle. Veek M <vek.m1234@gmail.com> - 2014-11-09 15:15 +0630
  Re: How do i reduce this to a single function - the code is largely similar, just a direction of search toggle. Denis McMahon <denismfmcmahon@gmail.com> - 2014-11-07 15:18 +0000

csiph-web