Python3.10以后版本中 collections中的Iterable被删除

文章介绍了如何在Python中使用isinstance函数配合collections.abc.Iterable模块来检查一个对象是否为可迭代对象。通过示例代码,展示了列表、字符串、元组和数字的不同响应,强调了123这样的数字不是迭代对象。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

判断对象是否为可迭代的对象:isinstance(obj, collections.abc.Iterable)

可以用collections.abc.Iterable代替collections.Iterable来判断是否为可迭代的对象

import collections

items = [1, 2, 3, 'a', 'b']
result = isinstance(items, collections.abc.Iterable)
print(result)  # 返回True

name = 'zhangSan'
result = isinstance(name, collections.abc.Iterable)
print(result)  # 返回True

values = (1, 2, 3, 4)
result = isinstance(values, collections.abc.Iterable)
print(result)   # 返回True

value = 123
result = isinstance(value, collections.abc.Iterable)
print(result)   # 返回False, 说明123这个对象不是迭代对象

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值