Question about typing: ints/floats
Steven D'Aprano
steven at REMOVE.THIS.cybersource.com.au
Wed Mar 3 21:18:23 EST 2010
On Wed, 03 Mar 2010 17:06:01 -0800, Chris Rebert wrote:
> But yes, internally, Python converted the int to a float before doing
> the addition.
[pedantic]
To be precise, Python created a *new* float from the int, leaving the
original int alone. Because ints and floats are objects, if Python
actually converted the int to a float, this would happen:
>>> n = 1
>>> m = n
>>> x = 2.0 + n
>>> print x
3.0
>>> print n
1.0
>>> "abc"[m]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: string indices must be integers
and the world would rapidly be destroyed. Fortunately this does not
happen.
[/pedantic]
--
Steven
More information about the Python-list
mailing list