473,396 Members | 1,738 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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:
a = int('2.0')

Traceback (most recent call last):
File "<stdin>", line 1, in ?
ValueError: invalid literal for int(): 2.0

This does not seem very pythonic

VJ
Jul 18 '05 #1
9 1978
Vineet Jain wrote:
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:
a = int('2.0')

Traceback (most recent call last):
File "<stdin>", line 1, in ?
ValueError: invalid literal for int(): 2.0

This does not seem very pythonic


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 && ma*@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
Jul 18 '05 #2
In article <40***************@alcyone.com>, Erik Max Francis wrote:
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.


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
Jul 18 '05 #3
Joe Mason wrote:
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?


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

--
__ Erik Max Francis && ma*@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
Jul 18 '05 #4
In article <40***************@alcyone.com>, Erik Max Francis wrote:
Joe Mason wrote:
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?


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


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

Joe
Jul 18 '05 #5
Joe Mason <jo*@notcharles.ca> wrote in
news:sl****************@gate.notcharles.ca:
In article <40***************@alcyone.com>, Erik Max Francis wrote:
Joe Mason wrote:
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?


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


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


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.

Jul 18 '05 #6
In article <sl****************@gate.notcharles.ca>,
Joe Mason <jo*@notcharles.ca> wrote:
In article <40***************@alcyone.com>, Erik Max Francis wrote:
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.


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?


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.
Jul 18 '05 #7
On 6 Apr 2004 11:52:32 GMT, Duncan Booth <me@privacy.net> wrote:
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.


Even this is debatable, as it is possible to spot the error.
'.' in '2.1' True

Or, to be sure about it...
numstr = '2.1'
('.' in numstr) or ('E' in numstr.upper ())

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
Jul 18 '05 #8
My vote for QOTW:
You could argue that unit testing, the universal solvent,
would clean up this too.

Raymond
Jul 18 '05 #9
Joe Mason wrote:int out of this string.

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


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>


Jul 18 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: cv | last post by:
Hello All, I have used MultipartRequest like the following to upload images. MultipartRequest multi = new MultipartRequest(request, "../webapps/coreprogram/dealerlogos", 1024 * 1024); It...
4
by: Field | last post by:
Hi, the following snippet shows once executed this output: 2 2 I'd have rather expected this output: 2 10
3
by: Jon Bosker | last post by:
I have written an application that lives in the systray but when I try to close windows or logout it does not close my application and therefore prevents windows from shutting down or logging me...
4
by: Das | last post by:
Hi, I have made an application in ASP.net with C#. The application works fine with localhost. I have uploaded the site. I'm using web user controls in the form. but some of the button do not work...
3
by: Erik Greene | last post by:
I have written an application that contains a notifyicon that will not dispose or end when Windows shutsdown. I have seen other posts where people are experiencing the same issue (see post in...
6
by: jobs | last post by:
This code was working, but then stopped working. I don't think I completely understand it. I pass it a formview name and it would loop through checking the value of textboxes. problem is...
1
by: Newbie in ChiTown | last post by:
Here's my code: I am using MS Access and I am trying to update a table (InvoiceDetails) with data input by the user on a form. However, it does not update nor does it give me an error message. ...
11
by: Jim | last post by:
Hi, I want to schedule a Python program that reads the command line for input. However, when adding an argument to the command line Python will not pick it up when using Windows scheduled...
6
by: Tmuldoon | last post by:
Hi, I have some Javascript that works on IE 6-7 but not Firefox(FF). FF throws this error when clicked: Hello, Using Publisher 6.2 and Firefox 2.0
2
by: Trish | last post by:
I have been trying to create a local table from a linked Excel Spreadsheet using a simple query string like this: SELECT * INTO LocalTable FROM LinkedExcelSheet; If I use CurrentDB.Execute, I...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.