简体   繁体   English

交叉py2和py3检查元组类型的方式?

[英]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. 似乎六个库没有元组类型定义,我目前正在寻找以与Python 2和Python 3兼容的方式编写以下代码。

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

PS. PS。 Don't blame me for that line, I only try to port it, is not written by me ;) 不要因为那行而怪我,我只是尝试移植它,不是我写的;)

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. 这是简单,直接,清晰的,可以在2.x和3.x中使用,并且可以正确处理子类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM