Heisenberg strikes again!

rzed Dick.Zantow at lexisnexis.com
Wed Sep 10 17:25:05 EDT 2003


neeson wrote:
> Alright, I'm fairly new to Python, and this one has got me stumped.
> I've just started to write a cli program that'll use readline, and
> I've attached the relevant bits.
>
> Here's the mystery:  If I so much as /look/ at the list 'matches',
> readline stops working.  Run the program, hit tab at the prompt,
> you'll get 'one', which is incorrect, as there are in fact four
> possible completions.  Now comment out the line that I've marked
> (which, incidentally, does nothing).  /Now/ it works.
>
> Is there some very strange side effect to accessing a list element
> that I'm unaware of?  I've tried it in two different versions of
> Python.
>
> Any elightenment would be appreciated...
>
> Heath
>
> ps In terms of being useful, this program doesn't make any sense.
> I'm not trying to get it to work, I'm looking to understand why the
> commented line affects the rest of the code.
>
>
> #!/usr/bin/python
>
> import readline
> import sys
>
> commands = ["one", "two", "three", "four"]
> matches = []
>
> def comp(text, state):
>     if state == 0:
>         matches = []
>         n = len(text)
>         for cmd in commands:
>             if cmd[:n] == text:
>                 matches.append(cmd)
>     throwaway = matches[0]   # <--- Comment out this line

What is supposed to happen when state != 0? 'matches' will not exist
at that point.

>     return commands[state]
>
> readline.set_completer(comp)
> readline.parse_and_bind("tab: complete")
>
> while 1:
>     try:
>         line = raw_input("> ")
>     except EOFError:
>         print "\n",
>         sys.exit()
>     print ": %s" % line






More information about the Python-list mailing list