[Python-Dev] PEP for Better Control of Nested Lexical Scopes

Steven Bethard steven.bethard at gmail.com
Tue Feb 21 22:47:42 CET 2006


On 2/21/06, Josiah Carlson <jcarlson at uci.edu> wrote:
> The question which still remains in my mind, which I previously asked,
> is whether the use cases are compelling enough to warrant the feature
> addition.

I don't know whether I support the proposal or not, but in reading
Mark Russel's email, I realized that I just recently ran into a use
case:
----------------------------------------------------------------------
# group tokens into chunks by their chunk labels
token_groups = []
curr_suffix = ''
curr_tokens = []
for token in document.IterAnnotations('token', percent=80):
 label = token[attr_name]
 # determine the prefix and suffix of the label
 prefix, suffix = label[0], label[2:]
 # B labels start a new chunk
 if prefix == 'B':
 curr_suffix = suffix
 curr_tokens = [token]
 token_groups.append((curr_suffix, curr_tokens))
 # I labels continue the previous chunk
 elif prefix == 'I':
 if curr_suffix == suffix:
 curr_tokens.append(token)
 # error: change in suffix - this should be a B label
 else:
 # log the error
 message = '%r followed by %r'
 last_label = curr_tokens[-1][attr_name]
 self._logger.info(message % (last_label, label))
 # start a new chunk
 curr_suffix = suffix
 curr_tokens = [token]
 token_groups.append((curr_suffix, curr_tokens))
 # O labels end any previous chunks
 elif prefix == 'O':
 curr_suffix = suffix
 curr_tokens = [token]
----------------------------------------------------------------------
You can see that the code::
 curr_suffix = suffix
 curr_tokens = [token]
 token_groups.append((curr_suffix, curr_tokens))
is repeated in two places. I would have liked to factor this out into
a function, but since the code requires rebinding curr_suffix and
curr_tokens, I can't. I'm not sure I care that much -- it's only
three lines of code and only duplicated once -- but using something
like ``curr_suffix :=`` or Phillip J. Eby's suggestion of
``.curr_suffix =`` would allow this code to be factored out into a
function.
STeVe
--
Grammar am for people who can't think for myself.
 --- Bucky Katt, Get Fuzzy


More information about the Python-Dev mailing list

AltStyle によって変換されたページ (->オリジナル) /