[英]How to determine the datatype in Python?
astring('a','tuple')
如何确定“x”是元组还是字符串?
if isinstance(x, basestring):
# a string
else:
try: it = iter(x)
except TypeError:
# not an iterable
else:
# iterable (tuple, list, etc)
@Alex Martelli的回答详细描述了为什么在使用Python中的类型时应该更喜欢上述样式(感谢@Mike Hordecki的链接)。
使用isinstance()
,一般语法是:
if isinstance(var, type):
# do something
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.