Posts: 212
Threads: 25
Joined: Aug 2019
(Oct-04-2019, 07:13 PM)metulburr Wrote: When i first encountered this i just Google'd how to do what i intended, and did it. I guess that in my case, being a newbie, it's a very vast and uncharted territory. So I'm not going only to find things that I don't understand completely, but I also need to look always for a better way to make my programs do what I want them to do.
All the best,
newbieAuggie2019
"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains."
Steve Jobs
Posts: 67
Threads: 17
Joined: Aug 2019
(Oct-04-2019, 06:59 PM)newbieAuggie2019 Wrote: I just wanted to see other points of view on this particular topic.
newbieAuggie2019 need not feel so apologetic about it. He deserves our thanks for bringing it up.
An open minded approach is regarded as a valuable feature of forums like this.
It would be most welcome if a future update of python ensures that int("4.5") does return 4.
Not being quite conversant with the required procedure, I would be thankful, if a suggestion on these lines could be put forth to python.org
A.D.Tejpal
Posts: 212
Threads: 25
Joined: Aug 2019
(Oct-05-2019, 03:30 AM)adt Wrote: (Oct-04-2019, 06:59 PM)newbieAuggie2019 Wrote: I just wanted to see other points of view on this particular topic. Just to say thank you for your understanding. I meant no offence when I brought it up.
All the best,
newbieAuggie2019
"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains."
Steve Jobs
Posts: 4,498
Threads: 69
Joined: Jan 2018
Oct-05-2019, 09:14 AM
(This post was last modified: Oct-05-2019, 09:14 AM by Gribouillis.)
I think a good reason for int('4.5') to raise an exception is that unicode strings appear in the interaction with human users, for example
np = int(input("How many persons are in the room? ")) By default, an answer such as 4.5 raises an exception and it seems to me the most consistent thing to do in the general case. It means that if you want your program to be tolerant to inaccuracies in numerical values, you need to implement it explicitly.
This may perhaps avoid such software errors. I don't think it's likely to change in the specification of the python language.
Note that nothing prevents you from defining
def int2(data):
try:
return int(data)
except ValueError:
return int(float(data))
Posts: 67
Threads: 17
Joined: Aug 2019
(Oct-05-2019, 09:14 AM)Gribouillis Wrote: By default, an answer such as 4.5 raises an exception and it seems to me the most consistent thing to do in the general case.
It might also be desirable to consider the likelihood that there could always be scope for further enhancement in python as evident by release of newer versions from time to time.
If experts in python community were to keep striving in their zeal (though well intended) to justify all that exists, one could as well surmise that we could well have continued with the original version released some decades ago.
Forums like this can play useful role in providing necessary feedback to python.org regarding suggested enhancements if any. It is up to them, in their wisdom to accept or not.
As an example of another inconsistency, let us look at the following:
s1 = "45"
s2 = "4.5"
print(int(s1)) # (A))
#>>>45
print(s1.isnumeric()) # (B)
#>>>True
print(float(s2)) # (C)
#>>>4.5
print(s2.isnumeric()) # (D)
#>>>False It would seem that statements (A) & (B) are consistent with each other, while the same could not be said for statements © & (D).
A.D.Tejpal
Posts: 8,090
Threads: 154
Joined: Sep 2016
Hey, let's not get carried away... With so many "inconsistencies" one would wonder how python made it so far... As far as something is explained in documentation it's up to programmer to use it accordingly
Posts: 4,498
Threads: 69
Joined: Jan 2018
Oct-05-2019, 02:38 PM
(This post was last modified: Oct-05-2019, 02:40 PM by Gribouillis.)
adt Wrote:If experts in python community were to keep striving in their zeal (though well intended) to justify all that exists You're strongly mistaking, it is my deep conviction that int('4.5') must raise an exception. It gives the program a chance to do take proper action if the string happens to contain a floating value, for example
while True:
s = input('Please enter the page number'))
try:
pageno = int(s)
except ValueError:
print('Error: an integer is required, not', repr(s)) But if int() accepts floating value, how can I recognize a floating value to reject it? I need to test it before the conversion to int and create another way to ensure it's an integer, something like
import re
while True:
s = input('Please enter the page number'))
if re.match('^\s*\d+\s*$', s):
pageno = int(s)
else:
print('Error: an integer is required, not', repr(s)) adt Wrote:It would seem that statements (A) & (B) are consistent with each other, while the same could not be said for statements © & (D). Again you're mistaking about the intent of the isnumeric() method. It doesn't mean that the string represents a numeric value, it means that all the characters in the string are numeric characters. It is a different purpose.
Posts: 212
Threads: 25
Joined: Aug 2019
(Oct-05-2019, 09:14 AM)Gribouillis Wrote: I think a good reason for int('4.5') to raise an exception is that unicode strings appear in the interaction with human users, for example
np = int(input("How many persons are in the room? ")) By default, an answer such as 4.5 raises an exception and it seems to me the most consistent thing to do in the general case. It means that if you want your program to be tolerant to inaccuracies in numerical values, you need to implement it explicitly.
It makes sense.
All the best,
newbieAuggie2019
"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains."
Steve Jobs
Posts: 67
Threads: 17
Joined: Aug 2019
(Oct-05-2019, 02:38 PM)Gribouillis Wrote: It is a different purpose.
Considering the fact that built in functions like isnumeric() , isdigit() , isdecimal() & isalnum() return false, can it be presumed that at present there is no built in function in python that can return True for a string like "4.5" ?
A.D.Tejpal
Posts: 212
Threads: 25
Joined: Aug 2019
Oct-05-2019, 04:13 PM
(This post was last modified: Oct-05-2019, 04:16 PM by newbieAuggie2019.)
(Oct-05-2019, 12:48 PM)adt Wrote: Forums like this can play a useful role in providing necessary feedback to python.org regarding suggested enhancements if any. It is up to them, in their wisdom to accept or not.
I would like to think that that could be possible.
All the best,
(Oct-05-2019, 01:12 PM)buran Wrote: Hey, let's not get carried away... With so many "inconsistencies" one would wonder how python made it so far... As far as something is explained in documentation it's up to programmer to use it accordingly
I guess that's also a very good point.
All the best,
newbieAuggie2019
"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains."
Steve Jobs
|