Python Forum
Inconsistency in Python programming language?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Inconsistency in Python programming language?
#11
(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
Reply
#12
(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
Reply
#13
(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
Reply
#14
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))
Reply
#15
(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
Reply
#16
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#17
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.
Reply
#18
(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
Reply
#19
(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
Reply
#20
(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

LOL 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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Programming robots using Python OscarBoots 5 3,463 Oct-31-2021, 09:38 AM
Last Post: Larz60+
  logo language code into python aayushi98 2 67,503 Jan-26-2021, 09:02 PM
Last Post: Serafim
  Programming Difficult math in Python Huntern 6 4,770 Oct-17-2019, 06:32 AM
Last Post: Huntern
  Terms describing Python Programming language leodavinci1990 3 2,734 Aug-12-2019, 02:48 PM
Last Post: leodavinci1990
  Please help a newbie choose which programming language to learn. yeto 2 3,504 Feb-25-2019, 12:56 AM
Last Post: yeto
  Python Programming Projects for Beginners jack_sparrow007 3 3,331 Dec-26-2018, 07:52 PM
Last Post: micseydel
  How to get image from WolframAlpha by using Python language manhnt 1 2,696 Oct-27-2018, 02:07 PM
Last Post: Larz60+
  Programming Python as a MS Windows app? Brian123 8 4,256 Oct-17-2018, 10:26 PM
Last Post: Brian123
  Help with Python programming mediaos 5 3,761 Aug-08-2018, 01:02 PM
Last Post: Larz60+
  How to make the python default language be 3.6 instead of 2.7 sylas 4 6,837 Jul-06-2018, 06:11 AM
Last Post: sylas

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020