setpythonhome_为什么在python中set(None)无效

因为set initializer接受了一个必须是iterable的对象,但是add()将使用任意元素,只要它是散列的。由于NoneType是不可编辑的,它引发了一个异常。在classset([iterable])

Return a new set or frozenset object whose elements are taken from iterable.

The elements of a set must be

hashable. To represent sets of sets, the inner sets must be frozenset

objects. If iterable is not specified, a new empty set is returned.

add(elem) Add element elem to the set.

iterable

An object capable of returning its members one at a time. Examples of iterables include all sequence types (such as list,

str, and tuple) and some non-sequence types like dict and file and

objects of any classes you define with an __iter__() or __getitem__()

method. Iterables can be used in a for loop and in many other places

where a sequence is needed (zip(), map(), ...). When an iterable

object is passed as an argument to the built-in function iter(), it

returns an iterator for the object. This iterator is good for one pass

over the set of values. When using iterables, it is usually not

necessary to call iter() or deal with iterator objects yourself. The

for statement does that automatically for you, creating a temporary

unnamed variable to hold the iterator for the duration of the loop.

See also iterator, sequence, and generator.In [18]: hash(None)

Out[18]: 39746304

In [19]: iter(None)

-

TypeError Traceback (most recent call last)

/home/jon/ in ()

TypeError: 'NoneType' object is not iterable

In [21]: dir(None)

Out[21]:

['__class__',

'__delattr__',

'__doc__',

'__format__',

'__getattribute__',

'__hash__',

'__init__',

'__new__',

'__reduce__',

...

'__subclasshook__']

In [22]: dir([])

Out[22]:

[...

'__hash__',

'__getitem__',

...

'__iter__',

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值