float和int的区别python_Python set类,float和int评估

I have not seen the implementation details of the set Class but I assume the answer to this is there somewhere. Python assignment basically evaluates rvalues and uses an identifier as reference object to point to the class object. Same for collections, i.e. they are an abstract data structure or a 'collection' of reference object. Sets don't allow duplicates and when I create a set like so:

s1 = {False, 1.0, 1, "a"} > {False, 1.0, "a"}

Float class wins over int class, obviously they evaluate to the same thing. But why does float show and init doesn't? I can't seem to find a decent answer or see it in the source.

As an aside I'd like to mention I noticed that True and False will be usurped in some way in place of 1 and 0 respectively if both are present using a .union() operation. So Floats win over Ints, and Ints win over Bools it would seem. But,

>>> s1 = {False, 'a', 1}

>>> s2 = {True, 'a', 0}

>>> s1 | s2

{False, 1, 'a'}

False remains.. I don't know if this was a REPL issue but after testing this again I get {0, 1, 'a'} every time I don't know what happened.

>>> s1 = {True, 'a', 1}

>>> s1

{1, 'a'}

>>> s2 = {False, 'a', 0}

>>> s2

{0, 'a'}

What am I missing?

>>> s1.union(s2)

{0, 1, 'a'}

解决方案The Boolean type is a subtype of the integer type, and Boolean values behave like the values 0 and 1, respectively, in almost all contexts, the exception being that when converted to a string, the strings "False" or "True" are returned, respectively.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值