문제

I am currently writing a big sudoku solver algorithm, and I have ran into a weird problem... somewhere deep in my code, I have this 'if' statement to check the type of a given variable. I want it to enter the if-statement if it is a list.

When I had code like:

if type(cell) == "list":
    # some code...

It wouldn't enter the statement (I have a print() that makes me sure of that... But with this:

if type(cell) == type(possibilities):
    # some code...

It does enter the code... 'possibilities' is another variable assigned earlier in the program that is ALWAYS a list. I also had print() statements before the 'if-statement' to tell me the current type of the cell, with:

print(type(cell))

and some printed, as expected, "< class "list" >"

What is the problem then? If you think it is needed, I may put more code here. I just thought it would be better not to since it is really big.

도움이 되었습니까?

해결책

You fix that as

if type(cell) == list:

of even better

if isinstance( cell, list ):

The latter works even if cell is of some derived type.

다른 팁

if type(cell) == list:

Notice, no quotes around list. list is a built-in variable referring to the list type.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top