sorin
sorin

Reputation: 170638

Cross py2 and py3 way of checking for tuple type?

It seems that six library does not have a tuple type definition and I am currently looking to write the code below in a way that works with both Python 2 and Python 3.

return (type(key) in (types.TupleType, types.ListType)

PS. Don't blame me for that line, I only try to port it, is not written by me ;)

Upvotes: 0

Views: 817

Answers (1)

Ned Batchelder
Ned Batchelder

Reputation: 375844

The best way would be:

return isinstance(key, (tuple, list))

This is simple, direct, clear, works in both 2.x and 3.x, and properly handles subclasses.

Upvotes: 4

Related Questions