Wilhelm
Wilhelm

Reputation: 1437

Why does 'if type(x) not list' return an error?

I would like to use if type(x) not list
instead of if not isinstance(x, list)
Am I missing something?

if not isinstance(x, list) seems excessive
since I'm not checking for class inheritance.

Upvotes: 0

Views: 65

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1124090

You forgot the is:

if type(x) is not list:

but if not isinstance(x, list) is not excessive, really. You should always allow for subclasses, it'll make your life easier at some point. Why close the door on that option?

Upvotes: 5

Related Questions