Connecting Tech Pros Worldwide Forums | Help | Site Map

int('2.1') does not work while int(float('2.1')) does

Vineet Jain
Guest
 
Posts: n/a
#1: Jul 18 '05
int('2.1') does not work while int(float('2.1')) does. If int can covert a
float object then there is no reason why a float string should not be
converted too.

When I do int('2.1') I get the following error:
[color=blue][color=green][color=darkred]
>>> a = int('2.0')[/color][/color][/color]
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ValueError: invalid literal for int(): 2.0

This does not seem very pythonic

VJ



Erik Max Francis
Guest
 
Posts: n/a
#2: Jul 18 '05

re: int('2.1') does not work while int(float('2.1')) does


Vineet Jain wrote:
[color=blue]
> int('2.1') does not work while int(float('2.1')) does. If int can
> covert a
> float object then there is no reason why a float string should not be
> converted too.
>
> When I do int('2.1') I get the following error:
>[color=green][color=darkred]
> >>> a = int('2.0')[/color][/color]
> Traceback (most recent call last):
> File "<stdin>", line 1, in ?
> ValueError: invalid literal for int(): 2.0
>
> This does not seem very pythonic[/color]

There's a fundamental difference between an int and a float. If the
string you're trying to convert looks like a float, that means it
doesn't look like an int.

With your first example of '2.1', what should it mean? Should it
truncate, round to negative infinity round to positive infinity, round
to zero, what? Python can't guess for you, so it shouldn't try. Thus
it's an error.

--
__ Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
/ \ San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
\__/ If I had another face, do you think I'd wear this one?
-- Abraham Lincoln
Joe Mason
Guest
 
Posts: n/a
#3: Jul 18 '05

re: int('2.1') does not work while int(float('2.1')) does


In article <40722555.66C70D40@alcyone.com>, Erik Max Francis wrote:[color=blue]
> There's a fundamental difference between an int and a float. If the
> string you're trying to convert looks like a float, that means it
> doesn't look like an int.
>
> With your first example of '2.1', what should it mean? Should it
> truncate, round to negative infinity round to positive infinity, round
> to zero, what? Python can't guess for you, so it shouldn't try. Thus
> it's an error.[/color]

Why can it make this guess for "int(2.1)", then? It's got a rule for
converting floats to ints - why not use it here?

Joe
Erik Max Francis
Guest
 
Posts: n/a
#4: Jul 18 '05

re: int('2.1') does not work while int(float('2.1')) does


Joe Mason wrote:
[color=blue]
> Why can it make this guess for "int(2.1)", then? It's got a rule for
> converting floats to ints - why not use it here?[/color]

Because int(aFloat) means round toward zero. int(aString) means make an
int out of this string.

--
__ Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
/ \ San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
\__/ Convictions are more dangerous enemies of truth than lies.
-- Friedrich Nietzsche
Joe Mason
Guest
 
Posts: n/a
#5: Jul 18 '05

re: int('2.1') does not work while int(float('2.1')) does


In article <40722E98.253B5FF2@alcyone.com>, Erik Max Francis wrote:[color=blue]
> Joe Mason wrote:
>[color=green]
>> Why can it make this guess for "int(2.1)", then? It's got a rule for
>> converting floats to ints - why not use it here?[/color]
>
> Because int(aFloat) means round toward zero. int(aString) means make an
> int out of this string.[/color]

So why can't int(string) mean round toward zero?

Joe
Duncan Booth
Guest
 
Posts: n/a
#6: Jul 18 '05

re: int('2.1') does not work while int(float('2.1')) does


Joe Mason <joe@notcharles.ca> wrote in
news:slrnc74t4i.ccv.joe@gate.notcharles.ca:
[color=blue]
> In article <40722E98.253B5FF2@alcyone.com>, Erik Max Francis wrote:[color=green]
>> Joe Mason wrote:
>>[color=darkred]
>>> Why can it make this guess for "int(2.1)", then? It's got a rule for
>>> converting floats to ints - why not use it here?[/color]
>>
>> Because int(aFloat) means round toward zero. int(aString) means make an
>> int out of this string.[/color]
>
> So why can't int(string) mean round toward zero?
>[/color]

If you want to round towards zero then you have an easy way to do it:

int(float(string))

If int(string) was changed to have this behaviour as well, then those of
who don't want any rounding wouldn't have any way to get the current
behaviour. Users may be surprised when they enter 2.1 and find the program
accepted it but didn't use the value they entered; I don't like suprising
users.

Or in more concise terms:

Explicit is better than implicit.

Mel Wilson
Guest
 
Posts: n/a
#7: Jul 18 '05

re: int('2.1') does not work while int(float('2.1')) does


In article <slrnc74a1l.bqf.joe@gate.notcharles.ca>,
Joe Mason <joe@notcharles.ca> wrote:[color=blue]
>In article <40722555.66C70D40@alcyone.com>, Erik Max Francis wrote:[color=green]
>> There's a fundamental difference between an int and a float. If the
>> string you're trying to convert looks like a float, that means it
>> doesn't look like an int.
>>
>> With your first example of '2.1', what should it mean? Should it
>> truncate, round to negative infinity round to positive infinity, round
>> to zero, what? Python can't guess for you, so it shouldn't try. Thus
>> it's an error.[/color]
>
>Why can it make this guess for "int(2.1)", then? It's got a rule for
>converting floats to ints - why not use it here?[/color]

I see it more as a programmer interface issue. There are
kinds of operation that can suggest (especially in a
dynamically-typed language) that your program has gotten
away from you. Python creator(s?) could have drawn this
particular line to this side or that, but in historical fact
they drew it here. Perhaps on the principle of one
conversion at a time, please.

You could argue that unit testing, the universal solvent,
would clean up this too.

Regards. Mel.
Stephen Horne
Guest
 
Posts: n/a
#8: Jul 18 '05

re: int('2.1') does not work while int(float('2.1')) does


On 6 Apr 2004 11:52:32 GMT, Duncan Booth <me@privacy.net> wrote:
[color=blue]
>If int(string) was changed to have this behaviour as well, then those of
>who don't want any rounding wouldn't have any way to get the current
>behaviour. Users may be surprised when they enter 2.1 and find the program
>accepted it but didn't use the value they entered; I don't like suprising
>users.[/color]

Even this is debatable, as it is possible to spot the error.
[color=blue][color=green][color=darkred]
>>> '.' in '2.1'[/color][/color][/color]
True

Or, to be sure about it...
[color=blue][color=green][color=darkred]
>>> numstr = '2.1'
>>> ('.' in numstr) or ('E' in numstr.upper ())[/color][/color][/color]
True


You claim it's a case of "Explicit is better than implicit" but I
don't know any typecast that is explicit about what it is casting from
in any language, Python included.


One way or the other, some users get a slight extra hassle. In this
case I think Python got it right for two reasons...

1. Wanting to implicitly accept a float in a string as an integer is
relatively unusual, so better to have the small extra hassle in
this case.

2. Accidentally accepting a float in a string as an integer when you
shouldn't is a bad thing - it is usually better to get a highly
visible exception early in development rather than releasing a
program which gives bad results without warning.

But it wouldn't matter than much either way. I've used at least one
language that did the conversion in one step and that never created a
serious problem. As Mel Wilson said...

: You could argue that unit testing, the universal solvent,
: would clean up this too.



--
Steve Horne

steve at ninereeds dot fsnet dot co dot uk
Raymond Hettinger
Guest
 
Posts: n/a
#9: Jul 18 '05

re: int('2.1') does not work while int(float('2.1')) does


My vote for QOTW:
[color=blue]
> You could argue that unit testing, the universal solvent,
> would clean up this too.[/color]


Raymond
Fredrik Lundh
Guest
 
Posts: n/a
#10: Jul 18 '05

re: int('2.1') does not work while int(float('2.1')) does


Joe Mason wrote:int out of this string.[color=blue]
>
> So why can't int(string) mean round toward zero?[/color]

because it isn't what it means.

why so obsessed with breaking existing code? don't you have
anything better to do with your life?

</F>




Closed Thread