[Tutor] Limitation of int() in converting strings

boB Stepp robertvstepp at gmail.com
Mon Dec 17 06:00:26 CET 2012


On Sun, Dec 16, 2012 at 10:41 PM, Mitya Sirenef <msirenef at lightbird.net> wrote:

> What would you want to happen for int("10.5")? If 10.0 was accepted,
> it would be consistent to accept 10.5, too.

I was expecting int("10.5") to return 10 .

> The issue, I think, is that a simple operation should not go too far
> beyond what it is supposed to do - if you are sure you are converting a
> float in a string, you need to do it explicitly, and if you're
> converting a string to an int and the string is not actually an int,
> then maybe it wasn't supposed to be a float and it's a mistake in the
> program -- and therefore python should alert you.
>
And this is why I asked the question. If this is the rationale, it
makes sense--an extra bit of double checking of the programmer's
intent.

Thanks,
boB


More information about the Tutor mailing list