[Tutor] Limitation of int() in converting strings

Mitya Sirenef msirenef at lightbird.net
Mon Dec 17 06:16:13 CET 2012


On 12/17/2012 12:00 AM, boB Stepp wrote:
> 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 .


I just want to note that this is what you expect _now_, because this is
what you're doing at the moment. If you were parsing a text and a float
turned up at an unexpected spot, you may well be unpleasantly surprised
if python silently changed it into a numerically quite different number!

>
 >
 >> 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.


No problem. I'm not certain this is the main/only reason, but I'd
expect this reason alone be enough to make this design choice..


  -m


-- 
Lark's Tongue Guide to Python: http://lightbird.net/larks/



More information about the Tutor mailing list