Skip to content

Instantly share code, notes, and snippets.

@luw2007
Created March 20, 2015 15:47
Show Gist options
  • Save luw2007/0569d155b07d33bdd7e2 to your computer and use it in GitHub Desktop.
Save luw2007/0569d155b07d33bdd7e2 to your computer and use it in GitHub Desktop.
python 混合类型升级 是一下升级完所有对象 还是边计算边升级对象?

##问: python 混合类型升级 是一下升级完所有对象 还是边计算边升级对象 如:1 +2.0+3 是把1 3 全部转换为浮点型在运算, 还是先转换1运算, 再将3转换和上面的结果进行计算

##曰: 从左到右运算。python里面都是二目运算符, 所以不用考虑3目的情况。 直接从左到右的逻辑。 计算到哪里,对符号两边的数转类型。 

In [2]: 1.0 + 2 + {}
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-2-119d787ad9c8> in <module>()
----> 1 1.0 + 2 + {}

TypeError: unsupported operand type(s) for +: 'float' and 'dict'

这里显示float + dict , 表示 1.0 + 2 已经被转成float 并且计算为3.0 (float类型)

In [3]: 1.0 + {}
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-3-b538d7a0760a> in <module>()
----> 1 1.0 + {}

TypeError: unsupported operand type(s) for +: 'float' and 'dict'

In [4]: 2 + {}
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-4-2abac09d3a6d> in <module>()
----> 1 2 + {}

TypeError: unsupported operand type(s) for +: 'int' and 'dict'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment