[英]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.