Question

When I create a list by calling the list function on an iterator that was itself produced with the itertools.combinations function, sometimes I get an error like the following:

*** Error in argument: '(combinations(aDict,2))'

But if I call the same list in a print, len or isinstance function, it works as expected.

I initially got this behavior in a script, but I could most reliably reproduce it using pdb, so the code example involves calling set_trace on a sript and then typing on the interpreter:

from itertools import combinations
aDict={'a':1,'b':2,'c':3}

print(len(list(combinations(aDict,2))))
print(isinstance(list(combinations(aDict,2)),list))
print(list(combinations(aDict,2)))

len(list(combinations(aDict,2)))
isinstance(list(combinations(aDict,2)),list)
list(combinations(aDict,2))

print('Hello')
import pdb
pdb.set_trace()

Then typing these commands in the interpreter:

>>> ================================ RESTART ================================
>>> 
3
True
[('a', 'b'), ('a', 'c'), ('b', 'c')]
Hello
--Return--
> c:\sw_dev\00_draft_development\02_scade\a429test\test_value_generator\pdbcomb.py(15)<module>()->None
-> pdb.set_trace()
(Pdb) len(list(combinations(aDict,2)))
3
(Pdb) isinstance(list(combinations(aDict,2)),list)
True
(Pdb) list(combinations(aDict,2))
*** Error in argument: '(combinations(aDict,2))'
(Pdb) len(list(combinations(aDict,2)))
3
(Pdb) 

So my question is, what is causing the error, and why does it not happen if I wrap the same command as an argument to len?

(I run python 3.3.1 over Windows 7 in a 64-bit Intel machine)

Était-ce utile?

La solution

list is a pdb command:

(Pdb) help list
l(ist) [first [,last]]
List source code for the current file.
Without arguments, list 11 lines around the current line
or continue the previous listing.
With one argument, list 11 lines starting at that line.
With two arguments, list the given range;
if the second argument is less than the first, it is a count.

Use an exclamation mark to evaluate the line as a Python expression: (Pdb) !list(combinations('abc', 2))

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top